프로그래밍/JSP Servlet

JSP/Servlet - 익셉션(exception) 타입별로 에러페이지 등록하기

가카리 2016. 1. 2. 21:25
반응형

 

익셉션 타입별로 에러페이지를 등록하는 방법은 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이면 다음과 같이 산술 연산 에러페이지로 이동하게 된다.

 

반응형