[JSP study]예외 처리

Noah97·2022년 5월 24일
0

JspStudy

목록 보기
10/18
post-thumbnail

1. page 지시자의 erroPage 속성 사용

JSP 페이지에서 에러를 처리하는 페이지를 지정하는 가장 간단한 방법은 page 지시자에서 errorPage를 지정하는 방법이다.
방법

<%@ page errorPage = "errorProcessing.jsp" %>

실행 예제
createError.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage = "errorProcessing.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String name = request.getParameter("name");
		if(name == null) {
			throw new NullPointerException(); //null일시 예외처리 발생 null이란 아무것도 없음을 의미
											  //모든 참조유형에 대한 기본값은 null
											  //null은 유효한 객체 인스턴스가 아니므로 할당 되는 메모리가 없다.
		}
	%>
</body>
</html>

errorProcessing.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isErrorPage="true"%>
	<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
img {
	width: 50px;
	height: 50px;
	float: left;
}
</style>
</head>
<body>
	발생한 예외 종류 :
	<%=exception.getClass().getName()%>
	<img src="error.png">
</body>
</html>

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

  • JSP 페이지에서 빈번하게 발생하는 에러 번호별로 에러 처리 페이지를 설정하는 방법니다. 서버 페이지 코드에서 오류는 대부분 500번 오류이다. 500 오류는 웹 서버에서 예기치 않은 일이 발생하고 서버가 더 구체적인 정보를 제공할 수 없을 때 나타나는 메시지이다.
  • 404 오류는 서버 자체는 존재하지만 서버에서 요청한 것을 찾을 수 없을 때 나타난다. 보통 HTTP에서 사용자가 요청하는 페이지나 파일을 찾을 수 없을 때 가장 많이 발생한다.
    예제
    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>404</error-code>
		<location>/404error.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/500error.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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%> //errorPage = "errorProcessing.jsp"를 삭제
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String name = request.getParameter("name");
		if(name == null) {
			throw new NullPointerException(); //null일시 예외처리 발생 null이란 아무것도 없음을 의미
											  //모든 참조유형에 대한 기본값은 null
											  //null은 유효한 객체 인스턴스가 아니므로 할당 되는 메모리가 없다.
		}
	%>
</body>
</html>

create404Error.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>
<a href="noPage.jsp">없는페이지 링크</a>
</body>
</html>

404error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%  response.setStatus(HttpServletResponse.SC_OK); %>
<html>
<head>
<title>404에러 페이지</title>
</head>
<body>
  요청하신 페이지는 존재하지 않습니다.
</body>
</html>

500error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isErrorPage="true"%>
	<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
img {
	width: 50px;
	height: 50px;
	float: left;
}
</style>
</head>
<body>
	발생한 예외 종류 :
	<%=exception.getClass().getName()%>
	<img src="error.png">
</body>
</html>

404에러 페이지 ⬇️

500에러 페이지 ⬇️

profile
안녕하세요 반갑습니다😊

0개의 댓글