접기
package servlet.dispatch;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * Servlet implementation class ErrorPronServlet */ public class ErrorPronServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public ErrorPronServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String choose = request.getParameter("choose"); if(choose == null){ //오류메세지를 request scope에 binding request.setAttribute("error_message", "체크박스가 선택되지 않았습니다."); //error.jsp로 수행을 이동 - request scope를 이용하므로 요청디스패치 방식으로 이동 RequestDispatcher rdp = request.getRequestDispatcher("/dispatch/error.jsp");// /로 시작하면 절대 경로 rdp.forward(request, response); return; } response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("체크박스가 체크되어 넘어왔습니다."+choose); out.println("</body></html>"); }
}
접기
접기
<%@ 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> 오류가 발생했습니다. 관리자에게 문의하세요.<br> 오류내용 : <% String errorMessage = (String)request.getAttribute("error_message"); %> <%=errorMessage %> </body> </html>
접기
접기
<!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> <form action="/myweb/error_pron_dispatch" method="post"> 체크 <input type="checkbox" name="choose" value="yes"> <input type="submit" value="전송"> </form> </body> </html>
접기
* 만약 redirect 방식으로 보내는 작업을 해주면 요청을 받고 응답을 하고 나면 바로 Attribute가 사라지므로 null 이 들어간다.
접기
package servlet.dispatch;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * Servlet implementation class ErrorPronServlet */ public class ErrorPronServlet extends HttpServlet { private static final long serialVersionUID = 1L;
public ErrorPronServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String choose = request.getParameter("choose"); if(choose == null){ //오류메세지를 request scope에 binding request.setAttribute("error_message", "체크박스가 선택되지 않았습니다."); // //error.jsp로 수행을 이동 - request scope를 이용하므로 요청디스패치 방식으로 이동 // RequestDispatcher rdp = request.getRequestDispatcher("/dispatch/error.jsp");// /로 시작하면 절대 경로 // rdp.forward(request, response); response.sendRedirect("/myweb/dispatch/error.jsp"); return; } response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("체크박스가 체크되어 넘어왔습니다."+choose); out.println("</body></html>"); }
}
접기