예외 처리

조수경·2022년 1월 18일
0

JSP

목록 보기
33/45

예외처리

프로세스는 실행중인 프로그램이며 프로세스가 실행되다 예외가 생기면 실행되다 다시 돌아옴
1. 프로그램이 처리되는 동안 특정한 문제가 발생 시 처리를 중단하고 다른 처리를 하는 것
(오류 처리)

2. Page 디렉티브를 이용
-errorPage 속성으로 오류 페이지 호출
<%@ page errorPage="오류 페이지 URL"%>
- isErrorPage 속성으로 오류 페이지 만듦(나는 오류 페이지야) default는 false(생략 시..)
<%@ page isErrorPage="true"%>
3. web.xml
- web.xml 파일을 통해 오류 상태(404, 500, 널오류)와 오류 페이지를 보여줌

   <error-page>
  			<error-code>오류 코드</error-code>
  			<location>오류 페이지 URI</location>
    	</error-page>
        
        <error-page>
  			<error-code>예외 유형</error-code>
  			<location>오류 페이지 URI</location>
    	</error-page>
  1. try-catch-finally
	try{
        실행문
    }catcn(처리할 예외 유형 설정){
    	예외 처리문
    }finally{
    	예외와 상관없이 무조건 실행(생략 가능)
    }

1. request.getRequestURL()

: 오류 발생 시 해당 오류 페이지 경로를 출력
http://localhost:8090/ch04/addProduct.jsp

2. request.getQueryString()

: 여기서 Query는 파람을 의미 / 요청 파라미터(productId=P1234)

결과 : http://localhost:8090/ch04/addProduct.jsp?productId=P1234

exceptionNoProductId.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>
<link rel="stylesheet" href="/css/bootstrap.min.css" />  
<title>상품 아이디 오류</title>
</head>
<body>
	<!-- top 인클루드 시작 -->
	<jsp:include page="/ch03/top.jsp" />
	<!-- top 인클루드 끝 -->
	
	<div class="jumbotron">
		<div class="container">
			<h2 class="alert alert-danger">해당 상품이 존재하지 않습니다.</h2>
		</div>
	</div>
	
	<div class="container">
		<p><%=request.getRequestURL()%>?<%=request.getQueryString()%></p>
		<p>
			<a href="/ch04/products.jsp" class="btn btn-secondary">
			상품 목록 &raquo;
			</a>
		</p>
	</div>
	
	<!-- bottom 인클루드 시작 -->
	<jsp:include page="/ch03/bottom.jsp" />
	<!-- bottom 인클루드 끝 -->
</body>
</html>

exceptionNoPage.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>
<link rel="stylesheet" href="/css/bootstrap.min.css" />  
<title>원하는 페이지가 없을때 오류</title>
</head>
<body>
<!-- top 인클루드 시작 -->
	<jsp:include page="/ch03/top.jsp" />
	<!-- top 인클루드 끝 -->
	
	<div class="jumbotron">
		<div class="container">
			<h2 class="alert alert-danger">요청하신 페이지를 찾을 수 없습니다.</h2>
		</div>
	</div>
	
	<div class="container">
		<p><%=request.getRequestURL()%></p>
		<p>
			<a href="/ch04/products.jsp" class="btn btn-secondary">
			상품 목록 &raquo;
			</a>
		</p>
	</div>
	
	<!-- bottom 인클루드 시작 -->
	<jsp:include page="/ch03/bottom.jsp" />
	<!-- bottom 인클루드 끝 -->
</body>
</html>

profile
신입 개발자 입니다!!!

0개의 댓글