JSP捕获404、500等错误页面并自定义

当网站页面找不到或者服务器内部出现错误的时候,我们不想让用户看到默认的那张 404,500 的错误页面,在web.xml文件中加入下面代码就可以自定义错误页面了

<error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
</error-page>

自定义404错误页面:

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>404 NotFound</title>
        <meta name="Keywords" content="404 NotFound"/>
        <meta name="Description" content="404 NotFound"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
        <link href="css/style_404.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript">
            setTimeout(function () {
                top.location = '${pageContext.request.contextPath}';
            }, 5000)
        </script>
    </head>
    <body>
        <div class="error404">
            <div class="info">
                <h1>404</h1>
                <h2>抱歉,您访问的页面不存在或已被删除!</h2>
                <p class="p1">5秒后将带您返回首页...</p>
                <a href="index.jsp" class="btn">返回首页</a>
                <a href="index.jsp" class="btn btn-brown">返回上一步</a>
            </div>
            <div class="pic">
                <img src="images/404.gif" alt=""/>
            </div>
        </div>
    </body>
</html>

效果如下:

猜你喜欢

转载自blog.csdn.net/weixin_41577923/article/details/83312280