[ JSP 에러페이지에 대하여... ]
jsp 페이지 처리 도중 404, 500에러 페이지가 뜨면 일반 사용자에게 불쾌감을 줄 수 있어
UI등을 꾸며놓은 친숙한 에러페이지를 띄우도록 할 수 있다.
두가지 방법이 있는데
1. page 지시자를 이용한 방법
2. web.xml에 error-page 를 등록하는 방법이 있다.
----------------------------------------------------------------------------------------------------------------------------
[ 1. page 지시자의 errorPage와 isErrorPage 속성을 이용한 방법 ]
main.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!-- [ 에러 페이지 ] 1 . page 지시자 errorPage 를 이용한 방법 2 . web.xml에 error-page 태그를 등록하는 방법 --> <!-- 1 번 해당 페이지에서 에러 발생시 띄워줄 에러 페이지를 page 지시자의 errorPage 속성에 명시해 준다. --> <% @page errorPage= "errorPage.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> <% int num = 4 / 0 ; // 고의적으로 에러 발생시킴 %> |
errorPage.jsp
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=UTF-8" pageEncoding= "UTF-8" %> <!-- 에러가 발생시 띄우게 되는 페이지에는 page 지시자의 isErrorpage = "true" 로 지정한다. 지정하지 않으면 default 가 false 이기 때문에 에러페이지로 인식하지 않는다. isErrorPage를 true 로 지정하면 exception 객체를 이용할 수 있게 된다. ex) exception.getMessage(); --> <% @page isErrorPage= "true" %> <!-- 에러 코드 200 번은 정상 페이지라는 의미인데, 가끔 자동으로 500 코드로 세팅되는 경우가 있어 현재 에러 페이지는 정상적인 페이지다라는 세팅을 해주기 위해 response.setStatus( 200 ); 을 지정해 준다. --> <% response.setStatus( 200 ); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> 에러페이지입니다. 에러 내용은 <%=exception.getMessage() %>입니다.<br/> |
[ 2. web.xml에 등록하는 방식 ]
main2.jsp
web.xml에 등록하는 방식일 때는 page지시자의 errorPage를 사용하면 안된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <%@ 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" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> <!-- web.xml을 이용해 예외페이지를 세팅할 때는 page지시자의 errorPage를 사용하면 안된다. --> <a href= "noPage.jsp" >없는페이지 링크</a> |
web.xml
error-page 태그내에 404에러일 때 띄울 페이지, 500에러일 때 띄울 페이지를 지정했다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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>ex3</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> <error-page> <error-code> 404 </error-code> <location>/error404.jsp</location> </error-page> <error-page> <error-code> 500 </error-code> <location>/error500.jsp</location> </error-page> </web-app> |
error404.jsp
실재 404 에러 발생시 띄울 페이지이다.
response.setStatus(200) 은 꼭 해주도록 하자. 자동으로 500으로 세팅되어 제대로 에러 페이지가 호출되지
않을 수 있기 때문
200번 상태코드는 현재 페이지가 정상적인 페이지라는 의미이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <% @page isErrorPage= "true" %> <% response.setStatus( 200 ); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> 404 에러페이지입니다. |
'스터디 > JSP' 카테고리의 다른 글
JSP DB(데이터베이스) (0) | 2017.08.14 |
---|---|
JSP 자바 빈즈(JAVA BEANS) (0) | 2017.08.14 |
JSP 쿠키(cookie)에 대하여... (0) | 2017.08.11 |
JSP에서의 세션(session)에 대해서... (0) | 2017.08.11 |
JSP 액션 태그(forward,include,param) (0) | 2017.08.10 |