Tomcat异常页面处理

时间:2025-03-28 19:47:30

在tomcat中修改 tomcat安装根目录–>conf–>配置文件就可以自定义错误发生时的提示页面,具体修改方法是在</web-app>上添加标签:

    <!-- 错误页面处理 -->
    <error-page>
        <error-code>404</error-code>
        <location>/</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/</location>
    </error-page>
    <error-page>
        <exception-type></exception-type>
        <location>/</location>
    </error-page>

其中<location>标签中即为自定义错误处理页面(或者说错误提示信息),一般可以这样写:

<%@ page language="java" contentType="text/html;charset=utf-8" %>
<%@ page isErrorPage="true" %>
<% response.setStatus(200); %>
<html>
    <body bgcolor="#ffffff">
        <h1>程序发生异常,我们将尽快修正,请多多包涵!<h1>
    </body>
</html>

其中加入<% (200); %>是为了确保该页面能正常显示。
*注:在程序开发阶段不推荐直接修改Tomcat的文件去自定义错误提示页面,可以先将文件中错误提示的标签注释掉,等程序发布时再恢复。