JSP| 포워딩
RequestDispatcher 클래스
- 받은 요청을 다른 컴퍼넌트에 위임
- 요청받은 컴퍼넌트와 위임받은 컴퍼넌트의 request 객체가 같다
- dispacherJsp.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
dispacherJsp.jsp
<hr />
id :
<%=request.getAttribute("id")%>
<br /> pw :
<%=request.getAttribute("pw")%>
<!--jsp에서도 RequestDispatcher를 생성해서 다른 곳으로 요청을 위임할 수 있음
RequestDispatcher dispatcher = request.getRequestDispatcher("/dispacherJsp.jsp");
dispatcher.forward(request, response); -->
</body>
</html>
- RequestObj.java
package com.javalec.ex;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestObj
*/
@WebServlet("/RequestObj")
public class RequestObj extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestObj() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doGet");
actionDo(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doPost");
actionDo(request, response);
}
private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("actionDo");
request.setAttribute("id", "abcde");
request.setAttribute("pw", "12345");
//생성 단계 getRequestDispatcher("/dispacherJsp.jsp") : 위임 받을 컴퍼넌트의 위치
RequestDispatcher dispatcher = request.getRequestDispatcher("/dispacherJsp.jsp");
dispatcher.forward(request, response);
}
}
HttpServletResponse
- RequestDispatcher와 의 차이점: 위임받은 컴퍼넌트와 요청받은 컴퍼넌트가 사용하는 request 객체가 다르다
- redirect.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("id", "abcde");
request.setAttribute("pw", "12345");
//HttpSevletResponse의 객체
response.sendRedirect("RequestObj");
%>
</body>
</html>
- RequestObj.java
package com.javalec.ex;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestObj
*/
@WebServlet("/RequestObj")
public class RequestObj extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestObj() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
actionDo(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
actionDo(request, response);
}
private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("actionDo");
//redirect.jsp와 request 객체가 다르므로 redirect.jsp에서 입력한 값이 아닌, null
String id = (String)request.getAttribute("id");
String pw = (String)request.getAttribute("pw");
response.setContentType("text/jhtml; charset=EUC-KR");
PrintWriter writer = response.getWriter();
writer.print("<html><head></head><body>");
writer.print("RequestObj" + "<hr />");
writer.print("id : " + id + "<br />");
writer.print("pw : " + pw);
writer.print("</body></html>");
}
}
Author And Source
이 문제에 관하여(JSP| 포워딩), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@kji306301/JSP-포워딩저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)