익셉션 타입별로 에러페이지를 등록하는 방법은 web.xml 파일에서 <error-page>라는 서브 엘리먼트를 쓰고 그 안에 다시
<exception-type>과 <location>이라는 두 개의 서브 엘리먼트를 쓰면된다.
이번 예제에서는 3개의 JSP파일과 web.xml 파일을 건드려야한다.
Divider.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String str1 = request.getParameter("NUM1");
String str2 = request.getParameter("NUM2");
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
int result = num1 / num2;
%>
<!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>
<%= num1 %> / <%= num2 %> = <%= result %>
</body>
</html>
Multiplyer.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String str1 = request.getParameter("NUM1");
String str2 = request.getParameter("NUM2");
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
int result = num1 * num2;
%>
<!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>
<%= num1 %> * <%= num2 %> = <%= result %>
</body>
</html>
NumberFormatError.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" 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>
에러페이지입니다 <%= exception.getMessage() %>
</body>
</html>
ArithmeticError.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" 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>
산술 연산 도중에 에러가 발생했습니다. <%= exception.getMessage() %>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ch5_jsp_exception2</display-name>
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/NumberFormatError.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/ArithmeticError.jsp</location>
</error-page>
</web-app>
실행 화면
입력 값이 정상일 때
입력 값이 비정상일 때
Divider.jsp파일도 입력 값이 비정상이면 동일한 에러페이지가 출력됨을 알 수 있다.
만약 나눠주는 값이 0이면 다음과 같이 산술 연산 에러페이지로 이동하게 된다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet - jsp 페이지 초기화 파라미터 설정하기 (0) | 2016.01.04 |
---|---|
JSP/Servlet - jspInit 메소드와 jspDestroy 메소드 활용 (0) | 2016.01.04 |
JSP/Servlet - 서블릿 초기화 파라미터 (0) | 2016.01.04 |
JSP/Servlet - init 메소드와 destroy 메소드 사용하기 (0) | 2016.01.03 |
JSP/Servlet - 서블릿 클래스에서 에러페이지 호출하기 (0) | 2015.12.30 |
JSP/Servlet - jsp 페이지에서 에러 발생시 다른 페이지로 이동하기 (0) | 2015.12.30 |
JSP/Servlet - JSP페이지에서 세션 사용하는 방법 (0) | 2015.12.29 |
JSP/Servlet - 서블릿 클래스에서 세션 기술 사용 방법 (0) | 2015.12.27 |