웹 프로그래밍 10(JSP)-예외처리를 해보자
*예외처리를 해보자
-예외처리 : 0으로 나누거나, 요청한 페이지가 없는 경우 등..
예외사항을 처리 하는 것이다.
-아래와 같은 상황을 많이 보았을것이다.
-위와 같이 경로가 유출되서 보안상 문제가 있을수도 있고 외관상 보기 좋지 않아
보통은 에러페이지를 따로 만들어 보여준다.
-예외처리 방법은 2가지가 있다.
1. 에러난 페이지에 예외처리를 등록 하는 방법
2. web.xml에 예외처리 등록 하는 방법
1. 에러난 페이지에 예외처리를 등록하는방법
-예전 [웹 프로그래밍 04(JSP)]에서 보여주었던 JSP태그가 기억을 되세겨보자
-해당 페이지 속성을 변경 할 수 있는 방법은 지시자를 사용하면 된다.
-에러 발생 페이지
1 |
<%@ page errorPage="errorPage.jsp" %> |
=>페이지 속성을 이용하여 에러가 발생시 errorPage.jsp로 이동한다.
-에러 처리 페이지
1 2 |
<%@ page isErrorPage="true" %> //에러 메소드를 사용 가능 <% response.setStatus(200); %> //에러 처리 페이지임으로 정상(200)페이지로 설정 |
=>주석 확인
-500에러 예제
main.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page errorPage="errorPage.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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<% int i=120301231/0; %>
</body>
</html> |
errorPage.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page isErrorPage="true" %>
<% response.setStatus(200); %>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
에러발생 하였습니다. 그이유는 다음과 같습니다.<br />
<%= exception.getMessage() %>
</body>
</html> |
-출력화면
2. web.xml에 예외처리 등록 하는 방법
-에러 페이지에 따로 설정하지 않아도 web.xml에 한번만 설정하면 간편히 등록이 가능하다.
-web.xml이 없다면 만들어준다
※ web.xml만드는방법
해당 프로젝트 오른쪽 클릭=>java EE Tools 클릭=> Gennerate Deployment Descriptor Stub 클릭
-다음과 같이 해당 에러 번호를 원하는 페이지로 연결이 가능하다.
1 2 3 4 5 6 7 8 9 |
<error-page> <error-code>404</error-code> <location>/404exception.jsp</location> </error-page>
<error-page> <error-code>500</error-code> <location>/500exception.jsp</location> </error-page> |
-예제
-main2.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body>
<form action="404error.jsp"> <input type="submit" value="404_error_page"><br /> </form>
<form action="500error.jsp"> <input type="submit" value="504_error_page"><br /> </form>
</body> </html> |
-404exception.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% response.setStatus(200); %> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> 404 오류 입니다. </body> </html> |
-500error.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body>
<% int i=192837912/0; %>
결과값은 <%= i %> 입니다.<br />
</body> </html> |
-500exception.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% response.setStatus(200); %> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> 500 오류 입니다. </body> </html> |
-web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>ex_errorpage</display-name> <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>
<error-page> <error-code>404</error-code> <location>/404exception.jsp</location> </error-page>
<error-page> <error-code>500</error-code> <location>/500exception.jsp</location> </error-page>
</web-app> |
-출력 화면
-main2.html
-404_error_page 선택
-505_error_page 선택
'프로그래밍 정리 > JSP, Servlet, DB(oracle)' 카테고리의 다른 글
웹 프로그래밍 12(DB)-오라클 시작하기 (0) | 2020.01.03 |
---|---|
웹 프로그래밍 11(JSP)-JSP에서 빈(Bean)을 사용해보자 (0) | 2020.01.03 |
웹 프로그래밍 09(JSP)-세션을 다루어 보자 (0) | 2020.01.03 |
웹 프로그래밍 08(JSP)-쿠키를 다루어 보자 (0) | 2020.01.03 |
웹 프로그래밍 07(JSP)-액션태그, jsp:include (0) | 2020.01.03 |