- 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
'Web > JSP' 카테고리의 다른 글
[필기정리]Day78 - ActionTag, JavaBeans, DAO, DTO, VO 등 (0) | 2020.10.20 |
---|---|
[개인실습] JSP로 회원형 게시판 만들기 (0) | 2020.10.19 |
[필기정리]Day76-3 - 저장영역(page, request, session, application) (0) | 2020.10.14 |
[필기정리]Day76-2 - ServletMapping, cookie 등 (0) | 2020.10.14 |
[필기정리]Day76-1 - doGet, doPost, service, 회원가입 구현 예시 등 (0) | 2020.10.14 |