設置經常使用錯誤頁面自定義顯示

web.xml根結點下增長html

 1     <error-page>
 2         <error-code>400</error-code>
 3         <location>/WEB-INF/jsp/error/400.jsp</location>
 4     </error-page>
 5     <error-page>
 6         <error-code>404</error-code>
 7         <location>/WEB-INF/jsp/error/404.jsp</location>
 8     </error-page>
 9     <error-page>
10         <error-code>406</error-code>
11         <location>/WEB-INF/jsp/error/406.jsp</location>
12     </error-page>
13 
14     <error-page>
15         <exception-type>java.lang.Throwable</exception-type>
16         <location>/WEB-INF/jsp/error/500.jsp</location>
17     </error-page>

編寫相應的錯誤文件(以500.jsp錯誤爲例)java

 1 <!-- 
 2 isErrorPage="true"必須設置,否則沒法獲取異常信息;
 3  -->
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     isErrorPage="true" pageEncoding="UTF-8"%>
 6 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 7 <%
 8     response.setStatus(HttpServletResponse.SC_OK); //這句也必定要寫,否則IE不會跳轉到該頁面
 9     String path = request.getContextPath();
10 %>
11 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
12 <html>
13 <head>
14 <meta name="renderer" content="webkit">
15 <title>500</title>
16 </head>
17 <body>
18     系統內部錯誤,請聯繫客服。<br> <br>
19     <div style="font-size: 14px; color: white;display:none">
20         錯誤發生頁面是:${pageContext.errorData.requestURI} <br> <br>
21         錯誤信息:${pageContext.exception} <br> <br> 錯誤堆棧信息:<br />
22         <c:forEach var="trace" items="${pageContext.exception.stackTrace}">
23             <p style="color:red;text-indent: 2em;">${trace}</p>
24         </c:forEach>
25     </div>
26 </body>
27 </html>
相關文章
相關標籤/搜索