JSP Servlet 기초(19) 포워딩(Forwarding)

이번엔 포워딩에 대하여 알아보겠습니다.


내가 어떠한 요청을 받았을때

그것을 내가 직접 처리하지않고

다른쪽으로 위임할때 포워딩 이라고한다


포워딩은 우리가 JSP 서블릿에서

2가지 클래스를 이용한다

RequestDispatcher 클래스와

HttpServletResponse 클래스를 이용한다


먼저 RequestDispatcher 클래스에대해 알아보자

이 클래스는 요청 받은 요청객체(request)를 위임하는

컴포넌트에 동일하게 전달 할 수 있다.


직접 테스트를 해보겠습니다

먼저 request.java 이름의 서블릿과

dispacher.jsp 파일 만들어줍니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.jsp.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;
 
@WebServlet("/request")
public class request extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public request() {
        super();
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        action(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        action(request, response);
    }
 
    private void action(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        request.setAttribute("id""asdfg");
        request.setAttribute("pw""12345");
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/dispacher.jsp");
        dispatcher.forward(request, response);
        
    }
}
 
cs


이렇게 서블릿에서 id값과 pw 값을 setAttribute 시켜줍니다

다음 이 값들을 33line 에서 dispacher.jsp 로 포워딩

시켜주는데 이때 request 객체와 response 객체를 같이 보내줍니다


다음 jsp 화면은

getAttribute로 id값과 pw값을 받아줍니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
        dispacherJsp.jsp
        <hr />
        
        id : <%= request.getAttribute("id")%> <br />
        pw : <%= request.getAttribute("pw")%>
 
</body>
</html>
cs


이제 request.java 파일에서

Ctrl + F11 눌러 실행시켜

출력값을 확인합니다



위와같이 주소창은 request.java 에서

실행 시켯기 때문에 request 이지만

화면은 dispacher.jsp인것을

확인할수 있습니다.


다음 HttpServletResponse 클래스 알아보겠습니다

HttpServletResponse 클래스는 

RequestDispatcher 클래스와 동일하게 요청을 위임하는

클래스 입니다. RequestDispatcher 클래스와 차이점은

요청 받은 요청 객체를 위임받은 컴포넌트에

전달 하는것이 아닌 새로운

요청객체를 생성합니다.


테스트 해보겠습니다.

먼저 request.java 서블릿과

redirect.jsp 파일 만들어줍니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.jsp.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;
 
@WebServlet("/request")
public class request extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public request() {
        super();
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        action(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        action(request, response);
    }
 
    private void action(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        
        String id = (String)request.getAttribute("id");
        String pw = (String)request.getAttribute("pw");
        
        response.setContentType("text/jhtml; charset=UTF-8");
        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>");
        
    }
}
 
cs


jsp 파일은 setAttribute 로 id값과

pw 값을 set 해준뒤 서블릿으로

Redirect 시켜줍니다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
 
        <%
            request.setAttribute("id""asdfg");
            request.setAttribute("pw""12345");
            
            response.sendRedirect("request");
       %>
 
</body>
</html>
cs

 

이제 redirect.jsp 파일에서 실행시켜줍니다



id값과 pw값에 asdfg, 12345 가 아닌

null 값이 찍힌다.


이것은 위에서 설명한 요청받은 요청객체를

위임받은 컴포넌트에 전달 하는 것이 아닌

"새로운" 요청객체를 생성 한다는것을

증명한다.



'JSP' 카테고리의 다른 글

JSP Servlet 기초(21) FrontController 패턴  (0) 2018.02.03
JSP Servlet 기초(20) url-pattern  (1) 2018.02.03
JSP Servlet 기초(18) JSTL 설치 사용  (0) 2018.02.02
JSP Servlet 기초(17) EL  (0) 2018.02.02
JSP Servlet 기초(16) 회원인증  (10) 2018.02.01

댓글

Designed by JB FACTORY