본문 바로가기
Server/JSP

[JSP] 예외 페이지 / 에러 처리 방법 (스크랩)

by 뒹굴거리는프로도 2017. 12. 22.
반응형

 


예외가 발생했을 때 에러 페이지를 호출하기 위한 방법은 3 가지가 있다.

1. page 디렉티브를 사용한 페이지 호출

2. web.xml의 error-code 태그 사용한 페이지 호출

3. web.xml의 exception-type 태그를 이용해 예외타입별로 페이지 호출

 

1.page디렉티브 사용한 페이지 호출


error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ page errorPage="errorCheck.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <%
    int num = 10/0;
  %>
</body>
</html>

12번 라인처럼 0으로 나누면 당연히 예외가 발생한다.

이 때 톰캣에서 제공하는 딱딱한 에러화면 말고 직접 에러페이지를 만들어서 보여주기 위해 3번 라인처럼 page 디렉티브를 이용했다. page 디렉티브의 errorPage 속성을 이용해서, 에러가 나면 errorCheck.jsp로 이동해라, 라고 알려주는 것이다.

실행을 해보면 Error.jsp에서 예외가 발생되어 에러페이지(errorCheck.jsp)가 출력된다.

 

errorCheck.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ page isErrorPage="true" %>
<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h2>0으로 나눌 수 없어요</h2>
에러메시지 : <%=exception.getMessage() %>
</body>
</html>

위에서 봐야 할 코드는 3,4번 라인이다.

3번 라인의 page 디렉티브의 isErrorPage속성을 true로 해줌으로써 이 페이지는 에러 페이지임을 알려준다.

그리고 true여야 13번 라인에서 사용된 exception 기본 객체를 사용할 수 있다. 기본값은 false이기 때문에, 지워버리면 exception객체를 사용할 수 없다.


4번라인을 사용한 이유를 정리해 보겠다.

웹 페이지는 status라는 상태값을 가지고 있다. 이 상태값으로 현재 웹 페이지가 정상인지 에러인지 알 수 있다.

우리가 만든 에러 페이지는, 에러라고 알려주기 위한 정상적인 페이지인데 간혹 status가 500인 상태로 설정이 되는 경우가 있다. (500은 서버 내부의 오류).

그래서 response.setStatus(200)을 이용해서 이 페이지는 정상적인 페이지라고 알려주는 거다.(200은 에러가 없이 정상적으로 전송되었음을 의미)

 

2. web.xml을 사용하기(error-code 태그 이용)


이번엔 page 디렉티브가 아닌 web.xml을 이용하는 방법이다. WEB-INF 폴더 아래에 web.xml 파일이 있다. 혹시 없다면 web.xml 파일을 만들어 준다.

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

error-code 태그에는 상태값을 세팅해주고, location 태그에는 해당 상태값일 경우 표시해줄 에러페이지의 위치를 설정해준다.

 

Error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <%
    int num = 10/0;
  %>
</body>
</html>

page 디렉티브는 사용하지 않으므로 지워줘야 한다.

이 소스코드를 실행하면 500에러가 나타나므로 error500.jsp를 보여줄 것이다.

 

error500.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h2>500에러 페이지 입니다.</h2>
</body>
</html>

 

404errorTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <a href="ABC.jsp">ABC.jsp</a>
</body>
</html>

ABC.jsp라는 페이지는 없으므로 404에러가 발생할 것이다.

 

error404.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h2>404에러 페이지 입니다.</h2>
</body>
</html>

 

3. exception-type 태그 이용하기


web.xml 파일에서 작업이 이루어진다. exception의 타입별로 에러페이지를 지정해 주는 것이다.

<error-page>
    <exception-type>java.lang.ArithmeticException</exception-type>
    <location>/error/error500.jsp</location>
</error-page>

이렇게 해주고, 위에서 해봤던 0으로 나눗셈하는 페이지를 실행해보면 정상적으로 에러페이지가 나올 것이다.

따라서 별도의 에러페이지가 필요하다면 page 디렉티브를 사용, 자주 사용되는 에러코드를 대비하려면 web.xml에서 error-code태그를 사용, 따로 처리해주어야 할 예외라면 web.xml에서 exception-type 태그를 사용하자.

 


 

반응형