/conf/web.xml에 에러 코드 유형별로 에러 페이지 설정 방법

<web-app>
    <error-page>
      <error-code>404</error-code>
      <location>/404error.jsp</location>
    </error-page>
    <error-page>
      <error-code>500</error-code>
      <location>/500error.jsp</location>
    </error-page>
    <error-page>
      <exception-type>java.lang.Throwable</exception-type>
      <location>/exception_error.jsp</location>
    </error-page>
</web-app>

 

100 : Continue 
101 : Switching protocols 
200 : OK, 에러없이 전송 성공
201 : Created, POST 명령 실행 및 성공 
202 : Accepted, 서버가 클라이언트 명령을 받음 
203 : Non-authoritative information, 서버가 클라이언트 요구 중 일부 만 전송 
204 : No content, 클라언트 요구을 처리했으나 전송할 데이터가 없음 
205 : Reset content 
206 : Partial content 
300 : Multiple choices, 최근에 옮겨진 데이터를 요청 
301 : Moved permanently, 요구한 데이터를 변경된 임시 URL에서 찾았음 
302 : Moved temporarily, 요구한 데이터가 변경된 URL에 있음을 명시 
303 : See other, 요구한 데이터를 변경하지 않았기 때문에 문제가 있음 
304 : Not modified 
305 : Use proxy 
400 : Bad request, 클라이언트의 잘못된 요청으로 처리할 수 없음 
401 : Unauthorized, 클라이언트의 인증 실패 
402 : Payment required, 예약됨 
403 : Forbidden, 접근이 거부된 문서를 요청함 
404 : Not found, 문서를 찾을 수 없음 

405 : Method not allowed, 리소스를 허용안함 
406 : Not acceptable, 허용할 수 없음 
407 : Proxy authentication required, 프록시 인증 필요 
408 : Request timeout, 요청시간이 지남 
409 : Conflict 
410 : Gone, 영구적으로 사용할 수 없음 
411 : Length required 
412 : Precondition failed, 전체조건 실패 
413 : Request entity too large, 
414 : Request-URI too long, URL이 너무 김 
415 : Unsupported media type 
500 : Internal server error, 내부서버 오류(잘못된 스크립트 실행시) 
501 : Not implemented, 클라이언트에서 서버가 수행할 수 없는 행동을 요구함 
502 : Bad gateway, 서버의 과부하 상태 
503 : Service unavailable, 외부 서비스가 죽었거나 현재 멈춤 상태 
504 : Gateway timeout 
505 : HTTP version not supported

에러페이지는 docbase 부터 시작됨

원문: http://blog.naver.com/neolithe/20024769222

 

conf/web.xml 파일에 다음과 같이 추가


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

<web-app>

...

...

 

  <!-- error page setup -->
 <error-page>
  <error-code>404</error-code>
  <location>/error/code404.jsp</location>
 </error-page>


 <error-page>
  <error-code>500</error-code>
  <location>/error/code500.jsp</location>
 </error-page>

...

...

</web-app>

 

 

설정하고 나서 code404.jsp , code500.jsp 페이지를 해당디렉토리에 만든다.

JSP 스크립트립 부분은 제외하고 나머진 알아서 디자인 하면된다.

 

code404.jsp


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

<%

response.setStatus(HttpServletResponse.SC_OK);

%>
<html>
<head>
<title>404 에러 페이지</title>
</head>
<body>
요청하신 페이지는 존재하지 않습니다.
</body>
</html>

 

code500.jsp


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

<%

response.setStatus(HttpServletResponse.SC_OK);

%>    
<html>
<head>
<title>500 에러 페이지</title>
</head>
<body>
서비스 과정에서 에러가 발생했습니다.<br>

빠른 시일안에 문제를 해결하겠습니다.
</body>
</html>

 

 

에러 발생 후 처리 예제

예) 로그인시 해당 아이디를 가진 사용자의 이름을 파싱하지 못하는 에러 발생.

 

infoForm.html


<!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>
<form method="post" action="viewInfo.jsp">
 이 름 : <input type="text" name="name" size="20"> <br>
 나 이 : <input type="text" name="age" value="20"> <br>
<hr>
* P.S : 나이는 숫자만 입력해야 합니다. 
<hr>
<input type="submit" value="전송">
</form>

</body>
</html>

 

viewInfo.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page errorPage="error02.jsp" %>
<%! 
String s_name; 
int age;

%>   
<%
//post 전송 방식일 경우 파라메터에 대한 한글 처리
request.setCharacterEncoding("UTF-8");

s_name = request.getParameter("name");
age = Integer.parseInt(request.getParameter("age"));

%>
<!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>
<h3> 회원 정보 출력 </h3><hr>
당신의 이름은 <%= s_name %>입니다.<br>
당신의 나이는 <%= age %>살입니다.<br>
</body>
</html>

 

error02.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page isErrorPage="true"%>   
<% 
response.setStatus(HttpServletResponse.SC_OK);
%>     
<!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>
<table align="center">
<tr bgcolor="#aaaaaa">
다음과 같은 에러가 발생하였습니다
</tr>
<tr>
<%= exception.getMessage() %>
</tr>
<table>
</body>
</html>

 [출처] http://www.cyworld.com/everysons/2352294

+ Recent posts