본문 바로가기

JAVA/JSP & Servlet

JSP 예외처리(Exception) 방법 3가지

입력화면

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

    pageEncoding="UTF-8"%>

   

<!-- ===========JSP 페이지에서 Exception 처리 방법 3가지============ -->

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

<h1>ex01_ok</h1>

<form action="ex01_ok.jsp" method="post">

        num1 : <input type="text" name="num1"><br>

        num2 : <input type="text" name="num2"><br>

<input type="submit" value="전송">

</form>

 

<br><br><br>

<h1>ex02_ok</h1>

<form action="ex02_ok.jsp" method="post">

        num1 : <input type="text" name="num1"><br>

        num2 : <input type="text" name="num2"><br>

<input type="submit" value="전송">

</form>

 

<br><br><br>

<h1>ex03_ok</h1>

<form action="ex03_ok.jsp" method="post">

        num1 : <input type="text" name="num1"><br>

        num2 : <input type="text" name="num2"><br>

<input type="submit" value="전송">

</form>

 

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

1 .try / catch => RequestDispatcher 를 이용한 에러페이지 출력 


ex01_ok.jsp

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

    pageEncoding="UTF-8"%>

   

<%

        int num1 = 0;

        int num2 = 0;

        int result = 0;

       

        try{

               String str1 = request.getParameter("num1");

               String str2 = request.getParameter("num2");

       

               num1 = Integer.parseInt(str1);

               num2 = Integer.parseInt(str2);

              

               result = num1 + num2;

        } catch(NumberFormatException e){

        //에러가 뜨더라도 url 'error_ex01.jsp' 가는것이 아니라 'ex01_ok.jsp' 나온다.

        //'ex01_ok.jsp' 에러페이지를 꽂아주는 형식

               RequestDispatcher dispatcher = request.getRequestDispatcher("error_ex01.jsp");

               dispatcher.forward(request, response);

        }

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<%=num1 %> + <%=num2 %> = <%=result %><br>

<a href="ex01.jsp">되돌아가기</a>

 

</body>

</html>

 

 

error_ex01.jsp

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

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

<h1>error_ex01.jsp</h1><br>

 

<h3>try / catch => RequestDispatcher 이용한 에러페이지 출력</h3><br>

 

잘못된 데이터가 입력되었습니다.<br><br>

<%="num1 : " + request.getParameter("num1") + "<br>" %>

<%="num2 : " + request.getParameter("num2") + "<br>" %>

<br><br>

<a href="ex01.jsp">되돌아가기</a>

 

 

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 2. errorPage="error_ex02.jsp" & isErrorPage="true"를 이용한 에러페이지 출력


ex02_ok.jsp

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

    pageEncoding="UTF-8" errorPage="error_ex02.jsp"%>

<!-- page 선언부에 errorPage="error_ex02.jsp" 추가 -->

<%

        int num1 = 0;

        int num2 = 0;

        int result = 0;

       

       

               String str1 = request.getParameter("num1");

               String str2 = request.getParameter("num2");

       

               num1 = Integer.parseInt(str1);

               num2 = Integer.parseInt(str2);

              

               result = num1 + num2;

       

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<%=num1 %> + <%=num2 %> = <%=result %><br>

<a href="ex01.jsp">되돌아가기</a>

 

</body>

</html>

 

 

error_ex02.jsp

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

    pageEncoding="UTF-8" isErrorPage="true"%>

<!-- page 선언부에 isErrorPage="true" 추가 -->

 

<%

        //200 => 브라우져 처리 코드

        response.setStatus(200);

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

<h1>error_ex02.jsp</h1><br>

 

<h3>errorPage="error_ex02.jsp"  &  isErrorPage="true" 이용한 에러페이지 출력</h3><br>

 

잘못된 데이터가 입력되었습니다.

 

<br><br>

 

<a href="ex01.jsp">되돌아가기</a>

 

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

3. web.xml 파일을 이용한 에러페이지 출력 


ex03_ok.jsp

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

    pageEncoding="UTF-8" %>

 

<%

        int num1 = 0;

        int num2 = 0;

        int result = 0;

       

       

               String str1 = request.getParameter("num1");

               String str2 = request.getParameter("num2");

       

               num1 = Integer.parseInt(str1);

               num2 = Integer.parseInt(str2);

              

               result = num1 + num2;

       

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<%=num1 %> + <%=num2 %> = <%=result %><br>

<a href="ex01.jsp">되돌아가기</a>

 

</body>

</html>

 

 

 

 

NumberFormatError.jsp

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

    pageEncoding="UTF-8" isErrorPage="true"%>

   

<%

        //isErrorPage 셋트로 써줌. 200 -> 브라우져 처리

        response.setStatus(200);

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

<h1>NumberFormatError.jsp</h1><br>

 

<h3>web.xml 이용하여 에러페이지 출력!</h3><br>

 

숫자 포맷이 잘못되었습니다.<br>

상세 에러 메세지 : <%=exception.getMessage() %>

 

<br><br>

 

<a href="ex01.jsp">되돌아가기</a>

 

</body>

</html>

 

 

 

 

NotFoundError.jsp

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

    pageEncoding="UTF-8"%>

<%

        response.setStatus(200);

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<h1>요청하신 페이지를 발견할 없습니다.</h1>

 

</body>

</html>

 

 

 

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

 

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

  version="3.0"

  metadata-complete="true">

 

  <display-name>Welcome to Tomcat</display-name>

  <description>Welcome to Tomcat</description>

 

  <error-page>

        <exception-type>java.lang.NumberFormatException</exception-type>

        <location>/NumberFormatError.jsp</location>

  </error-page>

 

        <error-page>

        <error-code>404</error-code>

        <location>/NotFoundError.jsp</location>

  </error-page>

 

</web-app>

 

 

 

 

 NumberFormatError

 

 

 

 

NotFoundError