tomcat配置错误页面

tomcat配置错误页面

如果希望定制工程的错误页面,有两种定义方式:

  1. 在web.xml中添加<error-page>
  2. 设置Host的errorReportValveClass属性

两种设置方法的区别在于:第一种是当前web应用生效,第二种是整个虚拟主机生效

除非错误页与web应用无关,否则不推荐配置成第二种。还有一个不适用此配置的原因是errorReportValveClass属性定义的错误页会出入服务器细节。不安全。

第一种定义方式配置方法

[root@iZzm446eh1ux98Z webapps]# vim myapp/WEB-INF/web.xml 
<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_3_1.xsd"
  version="3.1"
  metadata-complete="true">

<error-page> 
    <error-code>404</error-code>          
    <location>/err4.html</location>      ##其中"/" 对应的是Context标签中定义的docBase的位置
</error-page>
<error-page> 
    <error-code>500</error-code>
    <location>/err5.html</location>
</error-page>
</web-app>

编辑错误页

[root@iZzm446eh1ux98Z webapps]# vim myapp/err4.html 
Not Found

访问测试

原文地址:https://www.cnblogs.com/zh-dream/p/14846342.html