JSP 예외처리 (D-92)

최우정·2022년 5월 24일
0

100일

목록 보기
9/17
post-thumbnail

📒 1. page 지시자의 errorPage 속성 사용

JSP 페이지에서 에러를 처리하는 페이지를 지정하는 가장 간단한 방법은 page 지시자에서 errorPage를 지정하는 방법이다.
<@ page errorPagae = "errorProcessing.jsp" %>

createError.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>Insert title here</title>
</head>
<body>
	<%
	 String name = request.getParameter("name");
	 if(name == null){
		 throw new NullPointerException();
	 }
	%>
</body>
</html>

name 파라미터 값이 전송되지 않기 때문에 NullPointerException이 발생되며 특별히 에러 페이지 처리를 하지 않았기 떄문에 HTTP 상태 500 - 내부 서버 오류가 발생한다.

<% ~ %> 오류 부분 주석 처리

<%@ 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>Insert title here</title>
</head>
<body>
<%-- 	<%
	 String name = request.getParameter("name");
	 if(name == null){
		 throw new NullPointerException();
	 }
	%> --%>	
</body>
</html>

아무것도 뜨지 않고 발생하지 않는다.

속성(errorPage)추가

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="errorProcessing.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>Insert title here</title>
</head>
<body>
	<%
	 String name = request.getParameter("name");
	 if(name == null){
		 throw new NullPointerException();
	 }
	%>
</body>
</html>

errorProcessing.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 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>Insert title here</title>
</head>
<body>
발생한 예외 종류 : <%=exception.getClass().getName() %><br/>
<img src="danger.png" width: 50px;>
<!-- 사용 브라우저가 IE 일 경우에는 에러 페이지 크기가 513바이트 이상 되어야 인식된다. 
513 바이트 이상을 주석으로 만들어 주자
사용 브라우저가 IE 일 경우에는 에러 페이지 크기가 513바이트 이상 되어야 인식된다. 
513 바이트 이상을 주석으로 만들어 주자
사용 브라우저가 IE 일 경우에는 에러 페이지 크기가 513바이트 이상 되어야 인식된다. 
513 바이트 이상을 주석으로 만들어 주자
사용 브라우저가 IE 일 경우에는 에러 페이지 크기가 513바이트 이상 되어야 인식된다. 
513 바이트 이상을 주석으로 만들어 주자 -->
</body>
</html>

📒 2. web.xml에서 error-code 엘리먼트 값 설정

이 방법은 JSP 페이지에서 빈번하게 발생하는 에러 번호별로 에러 처리 페이지를 설정하는 방법이다. 서버 페이지 코드에서 오류는 대부분 500번 오류이므로 500번 오류 번호에 대해서 에러 페이지를 지정한다.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Chapter9</display-name>
  <error-page>
  	<error-code>500</error-code>
  	<location>/500.jsp</location>
  </error-page>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
</web-app>

createError.jsp 속성(errorPage) 삭제

<%@ 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>Insert title here</title>
</head>
<body>
	<%
	 String name = request.getParameter("name");
	 if(name == null){
		 throw new NullPointerException();
	 }
	%>
</body>
</html>

500.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	500 에러입니다.
</body>
</html>

NullPointerException 예외의 경우는 자주 발생하는 예외 타입이므로 예외 타입별로 에러 페이지를 저장하는 것이 효율적이다.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Chapter9</display-name>
  
<!--   <error-page>
  	<error-code>500</error-code>
  	<location>/500.jsp</location>
  </error-page> -->
  
  <error-page>
  	<exception-type>java.lang.NullPointerException</exception-type>
  	<location>/null.jsp</location>
  </error-page>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
</web-app>

null.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-eqive="Content-Type" content="text/html"; charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>NullPointerException</h1>
	발생한 예외 종류 : <%=exception.getClass().getName() %>
</body>
</html>

exception-type 엘리먼트 설정이 error-code 엘리먼트 설정보다 우선 순위가 높으므로 페이지를 실행하다가 NullPointerException이 발생하면 null.jsp 페이지에서 에러를 우선 처리하며, 그 이외의 서버 오류가 발생하면 500.jsp 페이지에서 에러를 처리한다.

profile
비전공자 Java, JavaScript, Html, Css, C++ 공부중

0개의 댓글