본문 바로가기

JAVA/JSP

JSP 에서의 오류처리 방법 및 예제

반응형


** jsp에서의 오류처리 방법

: jsp내에서 오류가 발생했을때를 대비하여 각 문서마다

try ~ catch로 할 수도 있고

일일이 각 문서마다 이렇게 하는 것은 번거로울 수 있다.

jsp에서는 오류처리를 효율적으로 하는 기법을 제공한다.



순서

1. 오류가 발생할 만한 jsp문서의 맨 상단의 page 지시자에 다음과 같이 써준다.

<%@ page errorPage="문서명" ~~~ %>

이 jsp문서에서 에러가 났을때 처리할 jsp문서이름을 써준다.

==> 이렇게하면 해당문서는 에러처리담당 문서가 된다.


2. 에러처리 담당 문서의 첫줄에는 다음과 같이 써준다.

("나는 에러처리 담당자 입니다" 라고 표시 해줘야 한다. 밑에처럼)

<%@ page isErrorPage="true" ~~ %>

이때 이 jsp문서의 서블릿에는 내장객체 exception이 생성된다.



** 가장 많이 발생하는 오류의 종류

문법오류 Status Code(SC) 500

존재하지 않는 문서를 요청하는 경우 404


예제


error500.jsp


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

pageEncoding="UTF-8"%>

<%@ page isErrorPage="true" %>    

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

서비스 중에 다음과 같은 문제가 발생하였습니다. <br>

<%= exception %>

<hr>

02-1234-5678로 전화 주시면 빠르게 조치하겠습니다.<hr>

</body>

</html>



error404.jsp

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

pageEncoding="UTF-8"%>

<%@ page isErrorPage="true" %>    

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

요청한 문서는 존재하지 않아요. <br>

확인 하시기 바랍니다. <br>

</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" 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>day0408A</display-name>


<error-page>

<error-code>500</error-code>

<location>/error500.jsp</location>

</error-page>


<error-page>

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

<location>/error404.jsp</location>

</error-page>


<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>

</web-app>




test.jsp


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

pageEncoding="UTF-8"%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

a = 3;

%>


<%= a %>

</body>

</html>





*오류처리 예제


ex13.jsp


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

pageEncoding="UTF-8"%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h2>사용자 정보 입력</h2>

<hr>

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

이름 : <input type="text" name="name"><br>

나이 : <input type="text" name="age"><br>

<input type="submit" value="확인">

</form>


<font color="hotpink">*PS : 나이는 꼭 숫자로 입력하세요!</font>


</body>

</html>




ex14.jsp


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

pageEncoding="UTF-8"%>

<%@ page errorPage="error.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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

request.setCharacterEncoding("UTF-8");

String name = request.getParameter("name");

int age = Integer.parseInt(request.getParameter("age"));

%>


<h2>사용자의 정보</h2>

<hr>

이름 : <%=name %><br>

나이 : <%=age %><br>

</body>

</html>




error.jsp


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

pageEncoding="UTF-8"%>

<%@ page isErrorPage="true" %>    

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

서비스 중에 다음과 같은 문제가 발생하였습니다.<br>

<%= exception %><br>

응급성이 있는 경우 02-123-4567로 전화하세요.

<hr>

</body>

</html>



반응형