์๋ธ๋ฆฟ ๋๋ JSP์์ ์์ฒญ์ ๋ฐ์ ํ์ ๋ค๋ฅธ component๋ก ์์ฒญ์ ์์ํ ์ ์๋ค. ๊ทธ๊ฒ์ ํฌ์๋ฉ์ด๋ผํ๋ค.
ํฌ์๋ฉ ๋ฐฉ๋ฒ์๋ 2๊ฐ์ ํด๋์ค(RequestDispatcher, HttpServletResponse)๋ฅผ ์ด์ฉํ๋ค.
RequestDispatcher ํด๋์ค
์์ฒญ๋ฐ์ ์์ฒญ๊ฐ์ฒด(request)๋ฅผ ์์๋ฐ๋ ์ปดํฌ๋ํธ(S2)์ ๋์ผํ๊ฒ ์ ๋ฌํ ์ ์๋ค.
์์
dispatcher.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") %>
</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");
RequestDispatcher dispatcher = request.getRequestDispatcher("/dispacherJsp.jsp");
dispatcher.forward(request, response);
}
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/dispacherJsp.jsp");
dispatcher.forward(request, response);
ํด๋ผ์ด์ธํธ๊ฐ ์ค request๊ฐ์ฒด์์ getRequestDispatcher("์์๋ฐ์ ์ปดํฌ๋ํธ")
๋ก ์์ฑํด์ค๋ค.
dispatcher.forward
๋ก ์ ๋ฌํด์ค๋ค. ๋ ํ์ผ์์ ์ฌ์ฉํ๋ ํ์ผ์ด ๋์ผํ request๋ผ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
HttpServletResponse ํด๋์ค
Request๋ฅผ ์์ํ๋ ํด๋์ค์ด๋ค. ํ์ง๋ง RequestDispatcher ํด๋์ค์์ ์ฐจ์ด์ ์ ์์ฒญ๋ฐ์ ๊ฐ์ฒด๋ฅผ ์์ ๋ฐ๋ ์ปดํฌ๋ํธ์ ์ ๋ฌํ๋ ๊ฒ์ด ์๋ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
์์
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");
response.sendRedirect("RequestObj");
%>
</body>
</html>
response.sendRedirect("RequestObj");
๊ฐ HttpServletResponse ๊ฐ์ฒด์ด๋ค.
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();
// 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");
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>");
}
}
null๊ฐ์ด ๋จ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์์์ ํ๋ค๊ณ ํด์ ๋ฐ๋ก ์ ๋ฌ๋๋ ๊ฒ์ด ์๋๋ผ ํด๋ผ์ด์ธํธ๊ฐ ๋ค์ ์์ ๋ฐ์ ์ปดํฌ๋ํธ๋ฅผ ์์ฒญํ๋ค. ์๋ก์ด ๊ฐ์ฒด๊ฐ ์์ฑ๋์ด ์ ๋ฌ๋๋ฏ๋ก ์์ฒญ๋ฐ์ ์ปดํฌ๋ํธ์ setAttribute
ํด์ค ๊ฐ์ ์์๋ฐ์ Component๋ก ์ ๋ฌ๋์ง ์๋๋ค.