jsp定义全局的错误处理

1.在web目录下新建一个common文件夹,在common文件夹下建立500.jsp和404.html两个页面。500.jsp页面处理500错误,404.html用于处理404错误。两个文件的内容如下:

500.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>500错误处理页</title>
</head>
<body>
    500错误!系统正在抢修中...<br/>
    错误原因:<%= exception.getMessage() %>
</body>
</html>

404.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>404错误处理</title>
</head>
<body>
    亲,你需要访问的页面已经被移除。你是不是想访问首页。
</body>
</html>

2.在web.xml文件配置error-page属性,web.xml配置如下:

<?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_3_1.xsd"
         version="3.1">

    <error-page>
        <error-code>404</error-code>
        <location>/common/404.html</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/common/500.jsp</location>
    </error-page>

</web-app>
原文地址:https://www.cnblogs.com/FlySheep/p/4858603.html