[Web] 에러 페이지 처리하기 (게시판 구현)
2017. 4. 10. 09:34ㆍJava/web
HTTP 오류 코드에 대해 처리할 수 있도록 설정, JSP를 작성해보자
웹 개발을 하면서 브라우저로 출력된 에러 페이지를 자주 봤을 것이다. 이러한 에러 페이지는 웹 컨테이너가 제공하는 기본 에러 페이지로, 오류뿐만 아니라 디렉토리의 구조, 톰캣의 버전 등이 노출되므로 웹 보안이 취약해진다(참고1). HTTP 상태 코드(Status Code)에는 200(성공), 404(찾을 수 없음), 500(내부 서버 오류) 등 클라이언트 요청에 대한 응답 코드가 많으니 확인해보도록 하자. 브라우저의 개발자모드 > Network 에서 확인 가능하다.
web.xml에 404, 500오류에 대한 처리를 설정하는 '에러 페이지 처리' 주석 아래의 코드를 삽입한다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>bbs</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 프로젝트에 사용되는 파라미터 -->
<!-- application 내장 객체가 읽어들인다. -->
<context-param>
<param-name>pageSize</param-name>
<param-value>10</param-value>
</context-param>
<context-param>
<param-name>pageBlock</param-name>
<param-value>10</param-value>
</context-param>
<servlet>
<servlet-name>bbsServlet</servlet-name>
<servlet-class>com.edu.bbs.BBSServlet</servlet-class>
<init-param>
<param-name>bbsProperties</param-name>
<param-value>D:\Dev\education\bbs\WebContent\bbs.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>bbsServlet</servlet-name>
<url-pattern>*.bbs</url-pattern>
</servlet-mapping>
<!-- 에러 페이지 처리 -->
<error-page>
<error-code>404</error-code>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorPage.jsp</location>
</error-page>
</web-app>
WebContent/erroPage.jsp를 아래와 같이 작성한다. errorPage.jsp에서는 해당되는 status_code에 따라 다른 결과를 화면에 출력하게 된다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error Page</title>
</head>
<body>
<c:if test="${requestScope['javax.servlet.error.status_code'] == 404}">
<p>404(찾을 수 없음): 서버가 요청한 페이지를 찾을 수 없습니다.</p>
<p>예를 들어 서버에 존재하지 않는 페이지에 대한 요청이 있을 경우 서버는 이 코드를 제공합니다.</p>
</c:if>
<c:if test="${requestScope['javax.servlet.error.status_code'] == 500}">
<p>500(내부 서버 오류): 서버에 오류가 발생하여 요청을 수행할 수 없습니다.</p>
</c:if>
<a href="/bbs/list.bbs?pageNum=1">돌아가기</a>
</body>
</html>
[JSP 공통 에러 페이지 처리(Servlet Exception Handling) 참고1]
'Java > web' 카테고리의 다른 글
| [Web] 부트스트랩 적용하기 (게시판 구현) (0) | 2017.04.10 |
|---|---|
| [Web] 글 확인 화면 만들기 (게시판 구현) (4) | 2017.04.10 |
| [Web] 페이징 처리하기 (게시판 구현) (2) | 2017.04.07 |
| [Web] 리스트 화면 만들기 (게시판 구현) (0) | 2017.04.06 |
| [Web] 글쓰기 화면 만들기 (게시판 구현) (0) | 2017.04.05 |