设置常用错误页面自定义显示

web.xml根结点下增加

 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错误为例)

 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>
原文地址:https://www.cnblogs.com/yanglei-xyz/p/10968137.html