15. 예외 페이지
0. 목차
Chapter15. 예외 페이지
Ch15 - 1. 예외 페이지의 필요성
Ch15 - 2. page 지시자를 이용한 예외 처리
Ch15 - 3. web.xml 파일을 이용한 예외 처리
Ch15 - 1. 예외 페이지의 필요성
▶ 예외 페이지란?
▷ 다소 딱딱한 에러 페이지를 보다 친근한 느낌이 느껴지는 페이지로 유도하기 위한 것
▶ 예외 페이지의 필요성
▷ 친근한 예외 페이지를 띄움으로 사이트 단절을 예방
- JSP, Servlet에서 예외적인 상황이 발생했을 경우
- 웹 컨테이너(톰캣)에서 제공되는 기본적인 예외 페이지가 보여 진다면,
- 사용자로 하여금 뭔가 불쾌한 느낌이 들면서, 다시는 해당 사이트에 접속하지 않을 것
- 그래서 다소 딱딱한 에러 페이지를 보다 친근한 느낌이 느껴지는 페이지로 유도
▶ 예외 페이지 처리 방법 두가지
▷ page 지시자를 이용한 예외 처리
▷ web.xml파일을 이용한 예외 처리
Ch15 - 2. page 지시자를 이용한 예외 처리
▶ 페이지 속성을 이용하여 예외 페이지 만들어 주기
▷ 일반 페이지에 에러 시, 이동 할 파일 작성
// info.jsp
<%@ page errorPage="errorPage.jsp"%> // 에러가 나면 errorPage.jsp 여기로 가세요
▷ errorPage 작성
// errorPage.jsp
<%@ page isErrorPage="true"%> // 디폴트가 "false"라서 "true"로 작성하지 않으면 이 페이지가 동작하지 않음
<% response.setStatus(200); %> // 200 : 정상적인 상태, 200을 작성하지 않으면 500(연산 예외 발생)이라 인식할 수도 있음
▶ 실습
▷ info.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page errorPage="errorPage.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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
int i = 40/0; // 0으로 나누어서 예외 발생
%>
</body>
</html>
▷ errorPage.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page isErrorPage="true"%> // 에러 페이지임
<% response.setStatus(200); %> // 에러 페이지는 정상임
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
여기서 문제가 발생 했습니다.<br />
<%= exception.getMessage() %> // 어떤 예외가 발생했는지 보여 줌
</body>
</html>
Ch15 - 3. web.xml 파일을 이용한 예외 처리
▶ web.xml 파일을 이용하여 예외 페이지 만들어 주기
▷ web.xml 작성
// web.xml
<error-page>
<error-code>404</error-code> // 404 예외 발생 하면
<location>/error404.jsp</location> // error404.jsp로 가라
</error-page>
▶ 실습
▷ web.xml 작성
// web.xml
<error-page>
<error-code>404</error-code> // 404 예외 발생 하면
<location>/error404.jsp</location> // error404.jsp로 가라
</error-page>
<error-page>
<error-code>500</error-code> // 500 예외 발생 하면
<location>/error500.jsp</location> // error500.jsp로 가라
</error-page>
▷ error404.jsp 작성
// error404.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page isErrorPage="true" %>
<% response.setStatus(200); %>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
404 에러 입니다.
</body>
</html>
▷ error500.jsp 작성
// error500.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page isErrorPage="true" %>
<% response.setStatus(200); %>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
500 에러 입니다.<br />
</body>
</html>
▷ error01.jsp 실행
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
int i = 40/0; // 500 예외 발생
%>
</body>
</html>
▷ error02.jsp 실행
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="error03.jsp">error03.jsp</a> // error03.jsp 파일 없음 → 404 예외 발생
</body>
</html>