본문 바로가기

Servlet

에러상태 코드 select로 값을 넘기기 (웹으로 확인하기)

첫화면
SC_NOT_FOUND 선택시

 

 

 

html

<body>
<a href="hello?name='성춘향'">Hello Servlet move</a>
<br>
<h1>상태 코드 확인</h1>
 
<form action="hello" method="get">
<table>
<tr>
	<td>상태 코드</td>
	<td>
		<select name="code">
			<option value="200">SC_OK</option>
			<option value="404">SC_NOT_FOUND</option>
			<option value="500">SC_INTERNAL_SERVER_ERROR</option>
		</select>
	</td>
</tr>
</table>
<input type="submit" value="송신">
</form>
</body>

 

 

java

public class HelloServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("HelloServlet doGet");
	//	String name = req.getParameter("name");
	//	System.out.println("name:" + name);
		
		resp.setContentType("text/html; charset=utf-8");		
		PrintWriter pw = resp.getWriter();		
		pw.println("<html>");
		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");		
		pw.println("<body>");
		
		String code = req.getParameter("code");
		if(code.equals("200")) {
			pw.println("<p>200:SC_OK</p>");
		}
		else {
			if(code.equals("404")) {
				resp.sendError(HttpServletResponse.SC_NOT_FOUND, "못 찾겠다는 에러입니다");
			}
			else if(code.equals("500")) {
				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "코드가 틀렸다는 에러입니다");
			}			
		}		
		pw.println("</body>");		
		pw.println("</html>");		
		pw.close();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

	}	
}

 

 

xml

  <servlet>
  	<servlet-name>HelloServlet</servlet-name>
  	<servlet-class>sample04.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>HelloServlet</servlet-name>
  	<url-pattern>/hello</url-pattern>
  </servlet-mapping>