Web/JSP

[JSP] 예외 페이지

PAYJAY 2018. 10. 23. 19:45




예외 페이지



예외적인 상황이 발생했을 경우 웹 컨테이너에서 제공되는 기본적인 예외페이지가 사용자에게는 다소 불편할 수도 있다 (ex)Tomcat 예외페이지) 이러한 기본적으로 제공되는 페이지가 아니라 개발자가 제작한 페이지를 사용자에게 보여줄 수 있다.


1.     Page 지시자를 이용한 예외 처리

A.     <%@ page errorPage=”errorPage.jsp”%>

B.      해당 페이지에서 예외 발생 시 설정한 페이지로 이동

C.      에러페이지에서는 해당 페이지가 에러 발생시 보여지는 페이지라는 것을 명시를 해줘야한다.

D.     <%@ page isErrorPage=”true”%> è defaultfalse이므로 true로 바꿔줘야한다.

E.      <%= exception.getMessage()%> è 에러메세지 출력. page지시자에서 false로 설정 되어있으면 exception객체를 참조할 수 없다.

F.      <%response.setStatus(200)%> è 우리가 만드는 페이지는 에러를 출력해주기만 하는 페이지, 즉 정상적인 페이지이다. 해당 페이지가 정상인 페이지라는 것을 설정하기 위해 사용.


ex)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!—에러 발생 페이지-->
 
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%@ page errorPage="errorTest.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <%
        int i = 40/0;
    %>
</body>
</html>
 
cs

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!—에러 표시 페이지-->
 
<%@ page language="java" contentType="text/html; charset=EUC-KR"
 
    pageEncoding="EUC-KR"%>
 
    <%@ page isErrorPage="true" %>
 
    <% response.setStatus(200); %>
 
<!DOCTYPE html>
 
<html>
 
<head>
 
<meta charset="EUC-KR">
 
<title>Insert title here</title>
 
</head>
 
<body>
 
           에러 발생<br>
 
           <%=exception.getMessage() %>
 
</body>
 
</html>
cs

 

 

 

2.     Web.xml 파일을 이용한 에러 페이지 지정

A.     <error-page> : 에러페이지 지정.

B.      <error-code> : 에러상태 코드 지정. 코드별로 지정.

C.      <exception-type> : 익셉션을 지정한다. 익셉션 별로 지정.

D.     <location> : 에러 페이지 위치 지정.

 


ex)

<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>