JSP 中的 Error Page

在 web.xml 中配置

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/pages/error/404.jsp</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/pages/error/500.jsp</location>
</error-page>

有兩個要點:

  • 把 jsp 放在 WEB-INF 中可以使其不能直接被訪問。
  • 可以用 error-code 或者 exception type 來找示錯誤信息。

書寫 error page

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
<title>後臺錯誤</title>

<link rel="stylesheet" href="static/Endless1.5.1/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h3><a href="/BC3/"><img alt="真是抱歉!" src="static/img/sorry.png" style="width:80px;"></a><span class="label label-default">非常抱歉,後臺出錯了!點我返回...</span> </h3>

<br>
<table class="table">
    <tr>
        <td width="20%"><b>錯誤:</b></td>
        <td>${pageContext.exception}</td>
    </tr>
    <tr>
        <td><b>URI:</b></td>
        <td>${pageContext.errorData.requestURI}</td>
    </tr>
    <tr>
        <td><b>狀態代碼:</b></td>
        <td>${pageContext.errorData.statusCode}</td>
    </tr>
    <tr>
        <td><b>錯誤追溯:</b></td>
        <td>
            <c:forEach var="trace" items="${pageContext.exception.stackTrace}">
                <p>${trace}</p>
            </c:forEach>
        </td>
    </tr>
</table>
</div>
</body>
</html>
發佈了59 篇原創文章 · 獲贊 8 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章