error_page.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   <h1>sorry</h1>
   <%= exception.getMessage() %>
</body>
</html>

JSP 실행중 에러 → error_page.jsp로 forward

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page errorPage="error_page.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<a href="no_such_page.jsp"><%= 10/0 %></a>
</body>
</html>

HTTP 코드별 에러 페이지 지정

WEB-INF/web.xml 파일에 다음을 추가한다:

<error-page>
    <error-code>404</error-code>
    <location>/error404.jsp</location>
</error-page>

동작 방식

즉,

JSP 코드와 상관없이 서버가 자동으로 404를 error404.jsp로 넘겨준다.

  1. try~catch 미설정 시
  2. page 지시자 errorPage 미설정 시