예외처리
- 프로그램이 처리되는 동안 특정한 문제가 발생했을 때 처리를 중단하고 다른 처리를 하는 것
- 예외 : 처리해야하는 것
예외 처리 방법
- page 디렉티브 태그를 이용한 예외 처리
- web.xml 파일을 이용한 예외 처리
- try/catch/finally db 연동할 때 많이 사용
page 디렉티브 태그를 이용한 예외 처리
ex)
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page errorPage="errorPage_error.jsp"%> <!-- 오류가 발생하면 errorPage_error.jsp 이동 -->
<html>
<head>
<title>Exception</title>
</head>
<body>
name 파라미터 : <%=request.getParameter("name").toUpperCase()%>
</body>
</html>
web.xml 파일을 이용한 예외 처리
- web.xml 파일을 통해 오류 상태와 오류 페이지를 보여주는 방법
- web.xml은 WebContent/WEB-INF/ 폴더에 생성
-웹 페이지 많을 경우에 사용
web.xml 에러 추가 ex)
<error-page><!-- 요소 내에 처리할 오류 코드나 오류 유형 및 오류 페이지를 호출 -->
<error-code>500</error-code> <!-- 오류 코드 설정 -->
<location>/CH11/errorCode_error.jsp</location><!-- 오류 페이지 URL을 설정 -->
</error-page>
<error-page><!-- 요소 내에 처리할 오류 코드나 오류 유형 및 오류 페이지를 호출 -->
<exception-type>java.lang.Exception</exception-type> <!-- 자바 예외 유형 -->
<location>/CH11/errorCode_error.jsp</location><!-- 오류 페이지 URL을 설정 -->
</error-page>
try/catch/finally를 이용한 예외 처리
- 자바 예외 처리 구문
쉽게 배우는 JSP 웹 프로그래밍
송미영 지음 한빛 아카데미