一、说明
1、在Web工程开发过程中可能会经常遇见400/404/500类型的错误页面,对于开发人员而言见到这种提示页面多了可能觉得问题不大,但是实际运用当中是面向客户的,需要配置一个合理的自定义错误页面。
2、本文只提Tomcat设置400/404/500自定义错误页面,如果Springmvc Controller配置自定义错误页面的方式,可以参考下我的这篇博文 点击这里~
二、实现
1、/WEB-INF/web.xml
<!-- =======================================错误页配置开始====================================== -->
<!-- 400错误 -->
<error-page>
<error-code>400</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 404 页面不存在错误 -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 500 服务器内部错误 -->
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<!-- java.lang.Exception异常错误,依据这个标记可定义多个类似错误提示 -->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<!-- java.lang.NullPointerException异常错误,依据这个标记可定义多个类似错误提示 -->
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/error.jsp</location>
</error-page>
<!-- =======================================错误页配置结束====================================== -->
2、error.jsp
直接放在Web工程的webapp或WebContent根目录下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
尊敬的朋友,后台服务器有问题,工程师们正在努力抢修中,请稍后访问......
</body>
</html>