JSP - 오류페이지
예외 사항 처리
예외 사항 코드
200 : 정상.
404 : 페이지를 찾을 수 없음.
500 : 서버 내부 오류.
예외가 발생할 수 있는 페이지의 jsp page 지시자 부분에
errorPage 속성을 설정(error.jsp)
에러 페이지의 jsp page 지시자 부분에
isErrorPage="true" 속성을 설정
에러 페이지의 스크립틀릿 영역에 'response.setStatus(200);'
문장 작성.(페이지의 page 지시자 다음 위치에 작성)
response.setStatus(200);
- 브라우저 프로그램에 따라 500 코드일 경우 html 문서를
무시하고 브라우저 자체 오류 페이지를 출력(익스플로러 등)
500 코드이지만 html문서의 내용을 출력하기 위해
200 코드로 상태를 변경하여 처리.
예외 데이터의 출력
exception 기본 객체 활용.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>일부러 0으로 나누는 페이지</title>
</head>
<body>
<%
String str1 = request.getParameter("n1");
String str2 = request.getParameter("n2");
int n1 = Integer.parseInt(str1);
int n2 = Integer.parseInt(str2);
int result = n1 / n2;
%>
결과 : <%=result %>
</body>
</html>
n1과 n2를 나누라는 명령문을 실행했지만
n1과 n2의 값이 정해져있지않기때문에 오류페이지가 나옴
n1과 n2에 값을 넣어줬을때↓
n1이나 n2에 0을 넣어줘서 에러페이지를 호출↓byZeroError.jsp↓
byZeroError.jsp↓
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>0으로 나눈 오류 페이지</title>
</head>
<body>
0으로 나눗셈을 처리하였습니다.
</body>
</html>
isErrorPage="true" 에러페이지를 확인시켜줌
web.xml에 위 에러페이지를 기본값으로 설정
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_4_0.xsd" version="4.0">
<display-name>CookieNSession</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/byZeroError.jsp</location>
</error-page>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% response.setStatus(200); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>요청 페이지 없음</title>
</head>
<body>
요청하신 페이지가 없습니다.<br>
관리자에게 문의하세요.<br>
</body>
</html>