서버가 자기 자신에게 request 할 수 있다 - redirect , dispatcher

세션은 인증 정보 같은 것만 담아야한다.

다른 데이터 들은 req, resp 에 담아야하는데 redirect 리다이렉트를 하면 유저정보를 다른페이지에 넘길 방법이 없다.

그럴 때 dispatcher 디스패쳐를 사용한다 .

 dispatcher 디스패쳐는 req, resp 객체를 유지한다.

ex)

A페이지가 유저정보를 들고 있는데

B페이지로 이동할 때 유저정보가 필요없다면 redirect

B페이지로 이동할 때 유저정보가 필요하다면 dispatcher

 

--------------------------------------

 

1) Dispatcher 방식

주소가 바뀌지 않는다.

같은 request 영역을 공유한다.


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 DispatcherServlet
 */
@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DispatcherServlet() {
        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
		RequestDispatcher dispatcher = request.getRequestDispatcher("dispatcher.jsp");
		request.setAttribute("request", "requestValue");
		dispatcher.forward(request, response);
	}

}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	request 속성 값: <%= request.getAttribute("request") %>
</body>
</html>

 

 

 

 

2) Redirect 방식

주소가 바뀐다.

request 영역에 공유한 속성 값에 접근 할 수 없다.


import java.io.IOException;
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 RedirectServlet
 */
@WebServlet("/RedirectServlet")
public class RedirectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RedirectServlet() {
        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
		request.setAttribute("request", "requestValue");
		response.sendRedirect("redirect.jsp");
	}

}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	request 속성 값 :<%= request.getAttribute("request") %>
</body>
</html>

 

 

+ Recent posts