본문 바로가기

JSP/JSP Programming

[JSP] 예외 코드별 예외처리

반응형

■ 예외 코드별 예외처리

 -. 톰캣 서버는 다양한 예외 상황이 발생할 수 있는데 이러한 예외 상황에 대해서 예외 코드를 정리해 두었다.

에 러

설                    명

404

 -. 존재하지 않는 페이지를 요청하게 되면 404에러 코드를 출력한다.

500

 -. 500에러 코드는 서버 내부 에러코드이다.


 -. JSP는 웹 애플리케이션 단위당 "WEB-INF/web.xml"을 통해서 에러 상태를 보여줄 페이지를 지정할 수 있다.

    에러 코드에 대해서 보여줄 페이지는 다음과 같이 web.xml 파일에 지정할 수 있다.

형                      식

<?xml version = "1.0" encoding = "euc-kr"?>

<web-app...>

<error-page>

...

      <error-code>에러 코드</error-code>

      <location>에러 페이지의 URL</location>

</error-page>

...

</web-app>



# 예외 처리를 위한 web.xml 파일 작성하기

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <error-page>
        <error-code>500</error-code>
        <location>/ch11/code500errPage.jsp</location>
    </error-page>

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

 출력화면



 code500errPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    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>500 에러 페이지</title>
</head>
<body>
서비스 과정에서 에러가 발생했습니다.<br/>
빠른 시일 안에 문제를 해결하겠습니다.
</body>
</html>
 code404errPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    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>404 에러 페이지</title>
</head>
<body>
요청하신 페이지는 존재하지 않습니다.
</body>
</html>

 viewInfo.jsp (errorPage를 주석 처리 한경우)

<%@ 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");

    // infoForm.html 문서에서 넘어 온 파라미터 값을 얻어온다.
    s_name = request.getParameter("name");
   
    // age의 값은 정수(Integer) 형으로 변환한다.
    age = Integer.parseInt(request.getParameter("age"));
%>
<%--infoForm.html 문서에서 넘어 온 파라미터 값을 출력한다. --%>
<h3>회원 정보 출력</h3>
당신의 이름은 <%= s_name %> 입니다.<br/>
당신의 나이는 <%= age %>세 입니다.<br/>

 출력화면

 viewInfo.jsp (errorPage를 주석 처리 하지 않은 경우)

<%@ 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");

    // infoForm.html 문서에서 넘어 온 파라미터 값을 얻어온다.
    s_name = request.getParameter("name");
   
    // age의 값은 정수(Integer) 형으로 변환한다.
    age = Integer.parseInt(request.getParameter("age"));
%>
<%--infoForm.html 문서에서 넘어 온 파라미터 값을 출력한다. --%>
<h3>회원 정보 출력</h3>
당신의 이름은 <%= s_name %> 입니다.<br/>
당신의 나이는 <%= age %>세 입니다.<br/>

 출력화면

 viewInfo.jsp (errorPage를 올바르게 입력하지 않은 경우)

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

<%@ page errorPage = "error03.jsp" %>      <%-- errorPage를 올바르게 입력하지 않은 경우 --%>

<%-- "error02.jsp"를 "error03.jsp"으로 수정 --%>
<%!
    String s_name;
    int age;
%>
<%
    // post 전송 방식의 경우 파라메터에 대한 한글 처리
    request.setCharacterEncoding("UTF-8");

    // infoForm.html 문서에서 넘어 온 파라미터 값을 얻어온다.
    s_name = request.getParameter("name");
   
    // age의 값은 정수(Integer) 형으로 변환한다.
    age = Integer.parseInt(request.getParameter("age"));
%>
<%--infoForm.html 문서에서 넘어 온 파라미터 값을 출력한다. --%>
<h3>회원 정보 출력</h3>
당신의 이름은 <%= s_name %> 입니다.<br/>
당신의 나이는 <%= age %>세 입니다.<br/>

 출력화면


반응형