1. 에러 페이지 지정

- JSP 페이지를 처리하는 도중에 익셉션이 발생할 경우 에러 하면 대신 지정한 JSP 페이지를 보여준다.

- page 디렉티브의 errorPage 속성을 사용해서 지정한다.


2. 에러 페이지 작성

- 에러 페이지에 해당하는 JSP 페이지는 page 디렉티브의 isErrorPage 속성의 값을 "true"로 지정한다.

- exception 기본 객체를 사용할 수 있다.

- 익스플로러 같은 경우는 513 바이트 이하일 경우에는 자체적인 HTTP 오류 메세지 창을 출력한다.


<!--errorTest.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page errorPage="error.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>파라미터</title> </head> <body> name 파라미터 : <%= request.getParameter("name").toUpperCase() %> </body> </html>


<!-- error.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error</title>
</head>
<body>
	요청 처리 과정에서 에러가 발생하였습니다.<br>
	<br>
	에러 타입 : <%= exception.getClass().getName() %><br>
	에러 메세지 : <%= exception.getMessage() %>
</body>
</html>



3. 주요 응답 상태 코드

- 200 : 요청이 정상적으로 처리 됨

- 307 : 임시로 페이지가 리다이렉트 됨

- 400 : 클라이언트의 요청이 잘못된 구문으로 구성 됨

- 401 : 접근이 허용되지 않음

- 404 : 지정된 URL 을 처리하기 위한 자원이 존재하지 않음

- 405 : 요청된 메서드는 허용되지 않음

- 500 : 서버 내부 에러 (JSP에서 익셉션이 발생하는 경우)

- 503 : 서버가 일시적으로 서비스를 제공할 수 없음 (급격하게 부하가 몰리거나 서버가 임시 보수 중인 경우)



4. web.xml 파일 에러 페이지 지정

- <error-page> 태그는 하나의 에러페이지를 지정한다.

- <error-code> 태그는 에러상태 코드를 지정한다. 에러 코드별로 지정한다.

- <exception-type> 태그는 익셉션을 지정한다. 익셉션별로 지정한다.

- <location> 태그는 에러 페이지의 위치를 지정한다.


<!-- web.xml -->

	<error-page>
		<error-code>404</error-code>
		<location>/error404.jsp</location>
	</error-page>
	
	<error-page>
		<error-code>404</error-code>
		<location>/error500.jsp</location>
	</error-page>
	
	<error-page>
		<exception-type>java.lang.NullPionterException</exception-type>
		<location>/errorNull.jsp</location>
	</error-page>


<!-- error404.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>404 에러</title>
</head>
<body>
	요청한 페이지는 존재하지 않습니다.
	<br><br>
	주소를 올바르게 입력했는지 확인하시길 바랍니다.
</body>
</html>


<!-- error500.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>500 에러</title>
</head>
<body>
	JSP 코드 에러 또는 DB 연결 실패가 발생되었습니다. 다시 시도해 보시기 바랍니다.
</body>
</html>


<!-- errorNull.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>NULL 에러</title>
</head>
<body>
	서비스 처리 과정에서 널(NULL) 에러가 발생하였습니다.
</body>
</html>



5. 에러 페이지의 우선 순위

- page 디렉티브의 errorPage 속성에서 지정한 에러 페이지를 보여준다.

- 발생한 익셉션 타입이 web.xml 파일의 <exception-type>에서 지정한 타입과 동일한 경우 지정한 에러페이지를 보여준다.

- 발생한 에러 코드가 web.xml 파일의 <error-code>에서 지정한 에러 코드와 동일한 경우 지정한 에러 페이지를 보여준다.

- 아무것도 해당되지 않을 경우 웹 컨테이너가 제공하는 기본 에러 페이지를 보여준다.


6. 에러 페이지 지정 형태

- 별도의 에러 페이지가 필요한 경우 page 디렉티브의 errorPage 속성을 사용해서 에러 페이지를 지정한다.

- 범용적인 에러 코드에 대해 web.xml에 에러 페이지를 지정한다.

- 별도로 처리해주어야 하는 익셉션 타입에 대해서는 web.xml에 별도 에러페이지를 지정한다.


7. 버퍼와 에러 페이지

- 최초로 버퍼가 플러시 될 때 응답헤더가 전송되는데 응답 상태 코드는 응답 헤더 앞에 전송된다.

- 버퍼가 최초로 플러시 될때까지 에러가 발생하지 않게되면 웹 브라우제어 200 응답 상태 코드가 전송된다.

- 에러 응답 코드 및 에러 페이지의 내용이 올바르게 웹 브라우저에 전송되기 위해서는 버퍼가 플러쉬 되면 안된다.

- 버퍼가 플러시 될 가능성이 있다면 버퍼 크기를 늘려 주어 에러가 발생하기 전에 버퍼가 플러시 되지 않도록 주의해야 한다.


<!-- bufferOverflow.jsp  -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page buffer="1kb" errorPage="error.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>버퍼 플러시 이후 에러</title>
</head>
<body>
	<%
		for (int i = 0; i < 256; i++) {
			out.println(i);
		}
	%>
	
	<%= 1 / 0  %>
</body>
</html>


+ Recent posts