입력화면
<%@ 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 를 이용한 에러페이지 출력
<%@ 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 파일을 이용한 에러페이지 출력
<%@ 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
'JAVA > JSP & Servlet' 카테고리의 다른 글
JSP 변수의 범위 (0) | 2013.03.20 |
---|---|
서블릿(servlet)의 라이프 사이클 (getInitParameter, getContextPath) (2) | 2013.03.20 |
세션(session) URL 재작성 (0) | 2013.03.20 |
쿠키(cookie) & 세션(session) (1) | 2013.03.20 |
Servlet(서블릿) 생성 기본! [annotation] (0) | 2013.03.15 |