forward
ServletTest1
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/test1")
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// request
request.setAttribute("requestData", "dataR");
// context
ServletContext context = request.getServletContext();
context.setAttribute("contextData", "dataC");
// session
HttpSession session = request.getSession();
session.setAttribute("sessionData", "dataS");
// forwarding 하기
RequestDispatcher disp = request.getRequestDispatcher("/test2");
disp.forward(request, response);
System.out.println(request.getRequestURL());
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
request 을 통해서 forwarding 을 할 수 있습니다.
ServletTest2
package servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/test2")
public class ServletTest2 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// request
String requestData = (String) request.getAttribute("requestData");
// context
ServletContext context = request.getServletContext();
String contextData = (String) context.getAttribute("contextData");
// session
HttpSession session = request.getSession();
String sessionData = (String) session.getAttribute("sessionData");
System.out.println(request.getRequestURL());
response.getWriter().write("requestData: " + requestData);
response.getWriter().write("\ncontextData: " + contextData);
response.getWriter().write("\nsessionData: " + sessionData);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
해당 path 로 url 변경 없이 넘어가게 됩니다. request 데이터를 그대로 담고 넘어갑니다.
sendRedirect
ServletTest1
package servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/test1")
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// request
request.setAttribute("requestData", "dataR");
// context
ServletContext context = request.getServletContext();
context.setAttribute("contextData", "dataC");
// session
HttpSession session = request.getSession();
session.setAttribute("sessionData", "dataS");
// sendRedirect 하기
response.sendRedirect(request.getContextPath() + "/test2");
System.out.println(request.getRequestURL());
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
response.sendRedirect(location) 으로 페이지를 이동시킬 수 있습니다.
path 가 아니라 location 이기 때문에 contextPath 까지 붙여야 이동하고 싶은 앱내 페이지로 이동을 합니다.
ServletTest2
package servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/test2")
public class ServletTest2 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// request
String requestData = (String) request.getAttribute("requestData");
// context
ServletContext context = request.getServletContext();
String contextData = (String) context.getAttribute("contextData");
// session
HttpSession session = request.getSession();
String sessionData = (String) session.getAttribute("sessionData");
System.out.println(request.getRequestURL());
response.getWriter().write("requestData: " + requestData);
response.getWriter().write("\ncontextData: " + contextData);
response.getWriter().write("\nsessionData: " + sessionData);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
response 로 sendRedirect() 하는 것이기 때문에 request 에 세팅한 데이터는 없어지게 됩니다.
왜냐하면 request 는 하나의 response 를 만나면 소멸이 됩니다.
728x90
'🧶 𝗪𝗲𝗯 > Servlet' 카테고리의 다른 글
[Servlet] JSP 사용에서 MVC 패턴 (0) | 2022.10.04 |
---|---|
[Servlet] URL 매핑 (1) | 2022.10.04 |
[Servlet] Life Cycle (1) | 2022.10.04 |
[Servlet] Servlet 관련 객체 및 API (1) | 2022.10.04 |