Jsp

JSP 에러 페이지를 만들어보자!

먼지제거제 2020. 8. 26. 23:18

 

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>Classt</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>500</error-code>
  <location>/500errorPage.jsp</location>
  </error-page>
  
</web-app>

500errorPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>

<% response.setStatus(HttpServletResponse.SC_OK);%>
<html>
<head>
<meta charset="UTF-8">
<title>500 를 내보자!</title>
</head>
<body>

페이지가 없는데..?

</body>
</html>

 

testerror.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%

int num = 1;

 %>

 <%= num(10) %>

</body>
</html>

 

 

testerror 페이지는 500 에러가 떠야한다.

 

에러 처리해주는 부분을 xml 에 추가하여

 

500errorPage.jsp 페이지가 출력된다.

 

 

출력값

 

500errorPage.jsp

 

 

출력값

페이지