Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Tags more
Archives
Today
Total
관리 메뉴

기록

JSP 웹페이지 예외처리 해주기 본문

JSP

JSP 웹페이지 예외처리 해주기

9400 2023. 1. 13. 11:11

처리우선순위 
page 디렉티브 > web.xml > try-catch

 

예외처리방법

1. page 디렉티브 태그 이용

   errorPage / isErrorPage 속성을 이용
   errorPage -> 오류를 처리하는 jsp
   isErrorPage -> 에러페이지니? true/false  

                           디폴트는 false이다

<%@ page errorPage="errorPage_error.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isErrorPage="true" %>
<!-- isErrorPage : 현재 본 jsp 페이지는 오류 처리 페이지이다. 기본이 false -->
<!DOCTYPE html>
<html>
<head>
<title>Exception</title>
</head>
<body>
	<p>오류가 발생했습니다.</p>
	<!-- p.361 -->
	<!-- exception : JSP에서 제공해주는 오류처리용 기본 내장 객체 -->
	<p>예외유형 : <%=exception.toString() %></p>
	<p>예외유형 : <%=exception.getClass().getName() %></p>
	<p>오류메세지 : <%=exception.getMessage() %></p>
</body>
</html>

 

2. try-catch-finally

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Exception</title>
</head>
<body>
	<!-- try-catch-finally를 이용한 예외처리
		 - 자바의 예외처리 구문으로
		 - try 구문 : 예외가 발생할 수도 있는 코드를 작성
		 - catch 구문 : 오류가 발생할 수도 있는 예외상황을 예측하여
		 	           오류를 처리하는 코드를 작성
		 - finally : try 구문이 실행된 후 실행할 코드를 작성(생략가능) 
		  -->

	<!-- 요청URL : tryCatch_process.jsp?num1=12&num2=6
	요청파라미터(HTTP파라미터, QueryString) : num1=12&num2=6 -->
	
	<% //스크립틀릿
	
	try{ //먼저 실행됨. 만약 예외발생시 실행을 중단하고, 예외와 일치하는 catch블록의 내용을 실행.
	String num1 = request.getParameter("num1");
	String num2 = request.getParameter("num2");
	
	//문자를 숫자로 형변환하쟈
   	int a = Integer.parseInt(num1); //12
	int b = Integer.parseInt(num2); //6
	int c = a/b;
	out.print(num1+" / "+num2+" = "+c);  
	}catch(NumberFormatException e){
		
		//오류가 발생하면 tryCatch_error.jsp로 포원딩
			
		//1)forwarding : jsp 해석 -> 컴파일 -> html 리턴받음. 데이터를 전달 할 수 있음.
		//2)redirect : URL을 재요청. 데이터를 전달하기 어려움.

		//request객체와 response객체를 전달함
		//tryCatch+error.sp에서도 요청파라미터인 ?num1=12&num2=0울 사용할 수 있음.
		
		RequestDispatcher rd = request.getRequestDispatcher("tryCatch_error.jsp");
		rd.forward(request, response);
		
		
	}
	%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Exception</title>
</head>
<body>
<!-- tryCatch_process.jsp
	dispatcher.forward(request,response);
	전달받은 request객체를 사용할 수 있음.
	 -->
	 <p>잘못된 데이터가 입력되었습니다.</p>
	 <p>숫자1 : <%= request.getParameter("num1") %></p>
	 <p>숫자2 : <%= request.getParameter("num2") %></p>
</body>
</html>

 

3. web.xml 이용 

web.xml : 톰캣(서버)환경설정

 web.xml에 추가
 
 <!-- 오류 코드에(404,500)에 맞춘 오류처리 jsp 매핑 -->
  <error-page>
   <error-code>404</error-code>
   <location>/error/error404.jsp</location>
  </error-page>
  
  <!-- 500:프로그래밍 오류 -->
    <error-page>
   <error-code>500</error-code>
   <location>/error/error500.jsp</location>
  </error-page>
  
  <!-- 예외 유형에 따른 오류처리 jsp 매핑 -->
    <error-page>
  	<exception-type>java.lang.NullPointerException</exception-type>
  	<location>/error/errorNullPointer.jsp</location>
  </error-page>

 

만약 404에러가 난다면 => /error/error404.jsp 로감 

만약 500에러가 난다면 => /error/error500.jsp 로감 

만약 nullpointer에러가 난다면 => /error/errorNullPointer.jsp 로감

 

error404.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>404오류</title>
</head>
<body>
	<img alt="404" src="/images/404.jpg" />
</body>
</html>

 

error500.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>500오류</title>
</head>
<body>
	<img alt="500" src="/images/500.jpg" />
</body>
</html>

 

errorNullPointer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<img alt="nullPointer오류" src="/images/nullPointer.png" />
</body>
</html>

 

 

예외발생시켜보기

출력성공

 

 

오류종류

 

예외종류

Comments