[필기정리]Day77 - include(), forward() 등

Web/JSP

2020. 10. 17. 18:49

# RequestDispatcher

 

[서블릿/JSP] RequestDispatcher로 include() 하기

이전글 [서블릿/JSP] RequestDispatcher란. RequestDispatcher로 forward() 하기 RequestDispatcher의 include() 메서드 RequestDispatcher의 메서드 중 하나인 include() 메서드는 처리 흐름 제어를 특정 대상에..

dololak.tistory.com

- RequestDispatcher의 include() ex)import

   RequestDispatcher 메소드 중 하나

   처리 흐름 제어를 특정 대상에게 넘기고 대상이 처리한 결과를 현재 페이지에서 처리한 결과에 포함시키는 기능

public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException; 

    ↕

- RequestDispatcher의 forward() ex) 페이지 이동

  include는 페이지 이동 없이 결과값을 요청한 페이지 내에서 보여주지만,

  forward는 페이지 이동을 통해 결과값을 보여준다.

 

ex)

- requestDispatcherInclude.jsp // include 예시

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="Bruce">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
requestDispatcherInclude.jsp 입니다.
<hr>
<%
//	out.flush();
	RequestDispatcher dispatcher = request.getRequestDispatcher("hello.jsp");
	dispatcher.include(request, response);
%>
</body>
</html>

- hello.jsp // include 결과

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="Bruce">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
hello.jsp 입니다.
</body>
</html>

- requestDispatcher.jsp // forward 예시

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="Bruce">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<% 
	request.setAttribute("name", "홍길동");
	RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
	dispatcher.forward(request, response);	
%>
</body>
</html>

- result.jsp // forward 결과

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="Bruce">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
이름 : <%=(String)request.getAttribute("name") %>
</body>
</html>

- sendRedirect.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="Bruce">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<%
	request.setAttribute("name", "홍길동");
	response.sendRedirect("result.jsp");
%>
</body>
</html>

 

728x90