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>
'Servlet' 카테고리의 다른 글
회원정보// .jsp파일로 넘기기 (0) | 2020.07.21 |
---|---|
select로 웹사이트 주소 송신하기 (0) | 2020.07.20 |
앙케이트 조사// 결과받아 web에 출력하기 (0) | 2020.07.20 |
servlet 코드소스 (0) | 2020.07.20 |
앙케이트 조사// 링크를 <button>을 통한 자바스크립트로 보냄 (0) | 2020.07.20 |