Intellij JSP - 에외 처리
1 minute read
에외 처리
-
Java Exception
-
make_error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<%
int num = 4/0;
%>
</body>
</html>
-
에러 발생 화면

-
에러 처리
<%
// try-catch를 추가
try {
int num = 4 / 0;
} catch (Exception e) {
out.print(e.getMessage());
}
%>
-
예외 처리 결과

-
Page 지시자
-
make_error_p.jsp
-
<%@ page errorPage=”error_page.jsp” %> - 에러 페이지 지정
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--Error Page 추가--%>
<%@ page errorPage="error_page.jsp" %>
<html>
<head>
</head>
<body>
<%
int num = 4 / 0;
%>
</body>
</html>
-
error_page.jsp
-
에러 페이지는 isErrorPage의 값을 “true” 로 변경
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<%
out.print(exception.getMessage());
%>
</body>
</html>
-
예외 처리 결과

-
web.xml
-
web.xml에 error page 추가
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
...
<!--추가-->
<error-page>
<error-code>404</error-code>
<location>/error/error_404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/error_500.jsp</location>
</error-page>
...
</web-app>
-
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<%
int num = 4 / 0;
%>
</body>
</html>
-
error_404.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isErrorPage="true" %>
<html>
<head>
<title>404 Error Page</title>
</head>
<body>
<h2>404 Error Page</h2>
</body>
</html>
-
error_500.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isErrorPage="true" %>
<html>
<head>
<title>500 Error Page</title>
</head>
<body>
<h2>500 Error Page</h2>
</body>
</html>
-
예외 처리 결과

-
요청한 페이지가 없을 때

-
에러 페이지에서 예외처리를 못하고 에러가 날 경우 아래의 코드 추가
<% response.setStatus(200); %>
-
Leave a comment