Dynamic Web Project를 basic05_mvc라는 이름으로 생성한다.
webapp 아래에 el, error, jstl 폴더를 생성하고 lib 폴더에 servlet-api.jar를 넣어준다.
error 폴더에 errorTest.jsp 파일을 생성한다.
server 탭에서 Tomcat 서버 우클릭 후 Add and Remove 클릭한 뒤 basic04_web을 내리고 basic05_mvc를 올려준다.
404 에러
errorTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>errorTest.jsp</title>
</head>
<body>
<h3>에러 메시지 확인</h3>
</body>
</html>
이 파일을 서버에서 런하고 errorTest.jsp 부분을 list.jsp로 바꾸면 아래와 같은 에러가 발생한다.
이러한 페이지를 사용자에게 보여주는 것보다는
에러 발생 시 사용자에게 보여줄 페이지를 만들어 이것을 보여주는 것이 더 좋다.
※ 참고
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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>basic05_mvc</display-name>
<!-- /WEB-INF/web.xml 배치관리자 -->
<!-- 환경설정에 관한 내용을 담고 있음 -->
<!-- ※ 주의사항 : web.xml이 수정되면 반드시 서버를 재시작해야 함 -->
<!-- 첫페이지(index.jsp) 자동 호출 등록 -->
<!-- 예)http://itwill.co.kr -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 세션시간 등록 (20분) -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<!-- 에러가 발생했을 때 이동할 페이지 지정 -->
<!-- HTTP 상태 404 - 찾을 수 없음 -->
<!-- 예)http://localhost:9090/basic05_mvc/error/list.jsp가 없는 페이지면 404 발생 -->
<error-page>
<error-code>404</error-code>
<location>/error/404code.jsp</location>
</error-page>
</web-app>
이제 list.jsp 페이지를 가보면 404 에러 화면이 나오지 않고 우리가 만들었던 404error.jsp를 보여준다.
500 에러
errorTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>errorTest.jsp</title>
</head>
<body>
<h3>에러 메시지 확인</h3>
<%
//에러가 발생하는 경우, 에러 메시지를 직접 출력하지 않고 다른 페이지로 이동할 필요가 있음
//-> WEB-INF/web.xml 배치 관리자에서 지정
//1) HTTP 상태 404 - 찾을 수 없음
//http://localhost:9090/basic05_mvc/error/list.jsp
//2)
int num = Integer.parseInt(request.getParameter("kor"));
out.print("num : " + num);
%>
</body>
</html>
이를 해결하기 위해서는 kor 값을 넘겨주어야 한다.
errorTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>errorTest.jsp</title>
</head>
<body>
<h3>에러 메시지 확인</h3>
<%
//에러가 발생하는 경우, 에러 메시지를 직접 출력하지 않고 다른 페이지로 이동할 필요가 있음
//-> WEB-INF/web.xml 배치 관리자에서 지정
//1) HTTP 상태 404 - 찾을 수 없음
//http://localhost:9090/basic05_mvc/error/list.jsp
//2) HTTP 상태 500 – 내부 서버 오류
//http://localhost:9090/basic05_mvc/error/errorTest.jsp
//-> ? 뒤에 Query String 값으로 kor값을 넘겨야 에러가 발생하지 않음
//-> 에러 해결 http://localhost:9090/basic05_mvc/error/errorTest.jsp?kor=30
int num = Integer.parseInt(request.getParameter("kor"));
out.print("num : " + num);
%>
</body>
</html>
500code.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>500code.jsp</title>
</head>
<body>
<!-- HTTP 상태 500 – 내부 서버 오류 -->
<h3>
이용에 불편을 드려 죄송합니다. 관리자에게 문의하세요
</h3>
</body>
</html>
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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>basic05_mvc</display-name>
<!-- /WEB-INF/web.xml 배치관리자 -->
<!-- 환경설정에 관한 내용을 담고 있음 -->
<!-- ※ 주의사항 : web.xml이 수정되면 반드시 서버를 재시작해야 함 -->
<!-- 첫페이지(index.jsp) 자동 호출 등록 -->
<!-- 예)http://itwill.co.kr -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 세션시간 등록 (20분) -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<!-- 에러가 발생했을 때 이동할 페이지 지정 -->
<!-- HTTP 상태 404 - 찾을 수 없음 -->
<!-- 예)http://localhost:9090/basic05_mvc/error/list.jsp가 없는 페이지면 404 발생 -->
<error-page>
<error-code>404</error-code>
<location>/error/404code.jsp</location>
</error-page>
<!-- HTTP 상태 500 – 내부 서버 오류 -->
<!-- 요청 파라미터 값이 없을 때 -->
<!-- http://localhost:9090/basic05_mvc/error/errorTest.jsp -->
<error-page>
<error-code>500</error-code>
<location>/error/500code.jsp</location>
</error-page>
</web-app>
'웹개발 교육 > jsp' 카테고리의 다른 글
[63일] jsp (44) - JSTL (0) | 2022.10.26 |
---|---|
[63일] jsp (43) - EL (0) | 2022.10.26 |
[63일] jsp (41) - MVC (0) | 2022.10.26 |
[62일] jsp (40) - myweb 프로젝트(호스팅) (0) | 2022.10.25 |
[61일] jsp (39) - myweb 프로젝트(회원 탈퇴 페이지) (0) | 2022.10.24 |