티스토리 뷰

FRONT-END/JSP

[JSP] 오류 예외처리

진심스테이크 2018. 4. 12. 15:49

 

오류 예외처리

 

주요 응답 상태 코드

- 200 : 요청을 정상적으로 처리

- 307 : 임시로 페이지를 리다이렉트함

- 400 : 클라이언트 요청이 잘못된 구문으로 구성

- 401 : 접근을 허용하지 않음

- 404 : 요청한 URL을 처리하기 위한 자원이 존재하지 않음

- 405 : 요청한 메소드를 허용하지 않음

- 500 : 서버 내부 에러가 발생 ex) JSP에서 익셉션 발생

- 503 : 서버가 일시적으로 서비스를 제공할 수 없음

 

 

에러 페이지 지정방법

 

1. error-code

<error-page>
      <error-code>404</error-code>
      <location>/404.jsp</location>
</error-page>

 

2. error-type

<error-page>
      <error-type>java.lang.NullPointerException</error-type>
      <location>/404.jsp</location>
</error-page>

 

 


 

 

설정 방법

 

1. 예외가 발생 했을 시, 실행되는 프로젝트 생성

 

파일 이름 작성 후 [Next >] 클릭

 

[Next >] 클릭

 

Generate web.xml deployment descriptor에 체크 한 후 [Finish] 클릭

-> web.xml 파일도 같이 생성 된다

 

 

 

프로젝트 생성 완료!

 

 

2. 위에 생성한 파일을 root로 지정

 

생성한 프로젝트에 마우스 오른쪽 클릭 후 [Properties] 클릭

 

[Web Project Settings] 에 Context root를 /로 지정 후 [Apply and Close] 클릭

 

 

3. 생성한 프로젝트의 web.xml 파일에 내용 추가

<?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" id="WebApp_ID" version="3.1">
  <display-name>aaaaa</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>400</error-code>
      <location>/400.jsp</location>
  </error-page>

</web-app>

 

4. web.xml 파일에서 작성한 <location>에서 지정 해 준 JSP 파일 생성

- WebContent 파일 안에 생성

 

[WebContent] 파일에서 마우스 오른쪽 클릭 -> [New] -> 맨 밑에 있는 [Other] 클릭

 

[Web] -> [JSP File] 선택하고 Next > 클릭

 

파일 이름.jsp 지정 후 [Finish] 클릭

5. JSP 파일 작성

- <body>에 오류 메시지 입력

<%@ 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>
응 오류야~
</body>
</html>

 

루트로 설정 해준 프로젝트 설정 완료!!

이제 내가 생성한 Web 프로젝트의 예외처리는 모두 aaaaa 프로젝트를 통해 예외 메시지를 받게 설정

 

6. 새로운 프로젝트 생성 후 JSP 파일 생성

#web.xml 파일은 생성하지 않아도 된다

 

test.jsp

<%@ 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>
빠빠 빨간맛! 궁금해 허니! 내가 제일 좋아하는건 여름 그 맛!!
</body>
</html>

 

 

7. 파일 실행 : Ctrl + F11

 

Tomcat 지정 후 [Next >] 클릭

 

Context root를 /로 지정해준 프로젝트와, 내가 실행시킬 프로젝트만 [Add >] 시킨 후 [Finish] 클릭

 

 

실행 결과

 

test.jsp 파일 실행

 

localhost:8080/ 뒤에 무작위로 입력

#8080은 HTTP/1.1 포트번호

 

test.jsp 파일이 있는 Error 프로젝트의 위치가 아니더라도

Context root를 /로 지정한 프로젝트로 넘어간 것을 볼 수 있다

 

 

0. 번외

localhost:8080/Error/ 뒤에 무작위로 입력했을 때에도 내가 지정한 메시지를 출력하고 싶으면, 그 프로젝트 안에  web.xml 파일과 <location>에 지정되어있는 파일을 생성해줘야 한다

 

 

web.xml 파일과 JSP 파일을 같은 위치에 복사

 

위에처럼 설정을 해 줘야만 localhost:8080/Error/ 뒤에 무작위로 입력했을 경우 메시지가 뜬다

 

 

 

☆완ㅡ료☆

 

 

 

 

 

'FRONT-END > JSP' 카테고리의 다른 글

[JSP] SENDREDIRECT( )  (1) 2018.04.13
[JSP] ATTRIBUTE & SCOPE - 속성과 영역  (0) 2018.04.12
[JSP] 한글 처리  (0) 2018.04.12
[JSP] 내장 객체  (1) 2018.04.12
[JSP] 구구단 테이블  (0) 2018.04.11
댓글