본문 바로가기

강의 정리/JSP & Servlet (Seoul Wiz)

15. 예외 페이지

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

 

예외 처리 전 코드

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		int i = 40/0;
	%>
</body>
</html>

 

예외 처리 전 작동 화면

 

 

 

 

 

예외 처리한 index.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page errorPage="errorPage.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		int i = 40/0;
	%>
</body>
</html>

 

 

 

예외 처리한 errorPage.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page isErrorPage="true" %> <!-- 이 페이지가 에러페이지임을 명시하고 eception 메소드를 사용 가능해짐 -->
<% response.setStatus(200); %> <!-- 기본값이 500으로 세팅되어 이 페이지 자체를 에러로 인식하는 경우가 있기 때문에 200을 명시 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	에러가 발생했습니다. <br><br>
	에러 원인: <%= exception.getMessage() %>
</body>
</html>

 

 

예외 처리 완료후 출력 화면

 

 

에러 안내 페이지에서 다음과 같이 200 처리를 해주지 않으면

<% response.setStatus(200); %>

 

에러 안내 페이지자체에 에러가 있는 것으로 인식하여 500에러가 발생한다

 

 

 

 

 

 

 

2. web.xml을 이용한 예외 처리

 

 

web.xml 파일에 에러 처리 코드를 추가

  <error-page>
  	<error-code>404</error-code> <!-- 에러 코드 -->
  	<location>/e404.jsp</location> <!-- 에러 발생시 넘겨줄 페이지 -->
  </error-page>
  <error-page>
  	<error-code>500</error-code>
  	<location>/e500.jsp</location>
  </error-page>

 

 

500에러를 발생시킬 index.jsp 

<%
	int i = 40/0;
%>

 

 

500에러 안내 페이지 e500.jsp

xml 처리 방식에서도 status 200은 명시해야 함을 주의

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
500 에러 페이지입니다.
</body>
</html>

 

 

500에러 안내 페이지 출력 화면

 

 

404에러를 발생시킬 index.jsp

존재하지 않는 페이지를 링크해 두었다

<a href="error03.jsp">go error03.jsp</a>

 

 

404에러 안내 페이지 e404.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
404 에러 페이지입니다.
</body>
</html>

 

 

index.jsp 출력화면

 

404에러 안내 페이지 출력 화면

 

 

 

 

'강의 정리 > JSP & Servlet (Seoul Wiz)' 카테고리의 다른 글

17. Oracle 데이터 베이스 1  (0) 2019.08.14
16. JAVA Bean  (0) 2019.08.14
14. 세션  (0) 2019.08.08
13. 쿠키  (0) 2019.08.06
12. 액션태그  (0) 2019.08.06