前言
- tomcat 9.0.45
配置异常/404页面
在web.xml文件中添加类似配置:
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
没有web.xml?自建一个。
不会建?tomcat自带的应用里有web.xml,复制粘贴一下(拿来用)。
错误页面可以是JSP,也可以是HTML。
来个示例
<?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"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- 404 页面不存在错误。同理,可以添加其它的错误码 -->
<error-page>
<error-code>404</error-code>
<location>/ErrorPage/404.html</location>
</error-page>
<!-- 400错误 -->
<error-page>
<error-code>400</error-code>
<location>/ErrorPage/error.html</location>
</error-page>
<!-- 500 服务器内部错误 -->
<error-page>
<error-code>500</error-code>
<location>/ErrorPage/error.html</location>
</error-page>
<!-- java.lang.Exception异常错误 -->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/ErrorPage/error.html</location>
</error-page>
<!-- java.lang.NullPointerException异常错误 -->
<error-page>
<exception-type>java.lang.NullPointerException </exception-type>
<location>/ErrorPage/error.html</location>
</error-page>
<!-- javax.servlet.ServletException异常错误。同理,可以添加其它的错误类型-->
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/ErrorPage/error.html</location>
</error-page>
</web-app>