JSP|転送


RequestDispatcherクラス


  • 受信要求を他の要素
  • に委任する.
  • 被要求要素の要求オブジェクトは、被許可要素の要求オブジェクトと同じ
  • である.
  • 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との違い:承認コンポーネントと要求コンポーネントが使用する要求オブジェクトが異なる
  • 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>");
    		
    	}
    
    }