ejyoo's 개발 노트

Error Page 설정 방법 본문

BackEnd/JSP_Servlet

Error Page 설정 방법

ejyoovV 2021. 6. 4. 19:23

Error Page 를 설정할 때 3가지 방법이 있다.

1) jsp 디렉티브에 에러페이지 설정

2) Handler(Servlet) 에 설정

3) web.xml에 설정

  - Excetion

  - Error 응답코드 부여

 

이 방법중 '3) web.xml' 에 설정하는 것을 가장 많이 사용하며

그 중 Error 응답코드 부여하는것을 많이 사용한다고 한다.

 

아래 예제는 숫자를 0으로 나누는 예외인 exception_arithmetic 기준으로 작성되었다.

 


1. jsp 디렉티브에 에러페이지 적용(속성 : errorpage, isErrorPage)

1) error를 발생시킬 'error_page.jsp' 를 생성한 후

디렉티브 영역에 'errorPage="/에러페이지"를 작성한다.

2) 에러 페이지인 'isErrorPage.jsp' 를 생성한 후 

디렉티브 영역에 'isErrorPage' 를 'true'로 설정한다.

만약 isErrorPage가 false인 경우, 에러페이지의 역할을 할 수 없다.

 

이 작업을 완료하면 isErrorPage.jsp 에 발생한 Exception에 대해 출력할 수 있다.

 

error_page.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="/isErrorPage.jsp"%>
<%@ page trimDirectiveWhitespaces="true" %>
<% System.out.println(1/0); %>

isErrorPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<%@ page trimDirectiveWhitespaces="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title></title>
</head>
<body>
<h1>Exception : <%=exception.getMessage() %></h1>
</body>

2. Handler Servlet 에 적용

1. jsp error 페이지 생성 방법과 비슷하게

try-catch를 사용하여

발생한 예외를 catch 한 뒤, exception 을 request에 담아 foward 하는 방식이다.

foward되는 jsp에서 에러 목록을 출력할 수 있다.

 

ErrorServlet.java

package com.jsp.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/error")
public class ErrorServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = "/success.jsp";
		try {
			System.out.println(1/0);
		} catch (Exception e) {
			url = "isErrorPage_servlet.jsp";
			request.setAttribute("exception",e);
		}
		
		request.getRequestDispatcher(url).forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

isErrorPage_servlet.jsp

본 코드에서는 EL문을 사용하였으나. 

<%= %>를 사용하여 출력해도 된다.(1. jsp 예외처리 방법 참고)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title></title>
</head>
<body>
<h1>Servlet forward Exception : ${exception.message}</h1>
</body>
</html>

 

3-1. web.xml에 설정 - Excetion

가장 많이 쓰는 방법인 web.xml 방법이다.

web.xml에 처리하고자 하는 예외를 작성한 뒤 해당하는 예외 발생 시, 설정한 페이지를 에러페이지로 출력한다.

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
	<error-page>
		<exception-type>java.lang.ArithmeticException</exception-type>
		<location>/error/exception_arithmetic.jsp</location>
	</error-page>
</web-app>

exception_arithmetic.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title></title>
</head>
<body>
<h1>exception_arithmetic!~~~</h1>
</body>
</html>

 

3-2. web.xml에 설정 - Error 응답코드 부여

가장 많이 사용하는 방법이다.

에러 코드를 직접 작성한 뒤 해당하는 에러코드 발생 시 에러페이지를 출력한다.

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
	<error-page>
		<error-code>500</error-code>
		<location>/error/500.jsp</location>
	</error-page>
	
	<error-page>
		<error-code>404</error-code>
		<location>/error/404.jsp</location>
	</error-page>
</web-app>

 

404.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title></title>
</head>
<body>
<h1>Error Page by web.xml : 404</h1>
</body>
</html>

500.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title></title>
</head>
<body>
	<h1>Error Page by web.xml : 500</h1>
</body>
</html>

 

 

예외 페이지 설정 시 참고사항

더보기

* sitemesh 라이브러리 사용한 경우

만약 web.xml에 작성한 에러페이지라면, sitemesh 로 설정한 템플릿이 적용되지 않는다. (부트스트랩 포함)

따라서 sitemesh를 적용할 수 없다. (sitemesh 필터를 적용하려면 response에 도달해야하는데, 톰캣이 에러페이지를 던져 response에 도달하지 않기 때문.)