Dispatcher
RequestObj.java (servlet)
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 {
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);
}
protected void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("actionDo");
request.setAttribute("id", "abcde");
request.setAttribute("pw", "12345");
RequestDispatcher dispatcher = request.getRequestDispatcher("/dispatcherTest.jsp");
dispatcher.forward(request, response);
}
}
dispatcherTest.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>
dispatcherTest.jsp
<hr>
id: <%=request.getAttribute("id") %><br>
pw: <%=request.getAttribute("pw") %>
</body>
</html>
결과:
dispatcherTest.jsp
id: abcde
pw: 12345
----------------------------------------------------------------------------------------
HttpServletResponse
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.setAttribute("id", "abcde");
request.setAttribute("pw", "12345");
response.sendRedirect("RequestObj2");
%>
</body>
</html>
RequestObj2.java
import java.io.IOException;
import java.io.PrintWriter;
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 RequestObj2
*/
@WebServlet("/RequestObj2")
public class RequestObj2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestObj2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @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 {
// 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>");
}
}
'기존카테고리 > Java & jsp Tip' 카테고리의 다른 글
Spring에서 한글깨짐 방지를 위해 web.xml에서 한글 설정 (2) | 2017.07.18 |
---|---|
getContextPath 와 getRequestURI (0) | 2017.07.08 |
asp Response.end 또는 php의 exit;와 같은 jsp의 if(true) return; (0) | 2017.07.07 |
체크 박스 받기 (0) | 2017.07.06 |
글자변환(String Replace) 과 날짜 표시 (변환?) (0) | 2017.06.29 |