zoukankan      html  css  js  c++  java
  • Tomcat中配置自定义404错误页面

    404,50x这种错误经常遇到。

    如果%CATALINA_HOME%confweb.xml和具体应用中都有设置%CATALINA_HOME%webappsROOTWEB-INFweb.xml

    则应用中的生效。

    如何设置一个友好的用户体验,以windows环境为例:

    添加发生异常时,默认的文件,文件位置如下(如果没有设置reloadable为true或autoDeploy="true",则需要重启tomcat让更改的web.xml生效):


    (1)%CATALINA_HOME%confweb.xml中web-app节点中添加

        <error-page>
            <error-code>404</error-code>
            <location>/notFileFound.jsp</location> <!--定义的页面,必须以“/”开头-->
        </error-page>


    效果展示:
    正常情况:

    出现404时的场景:

    %CATALINA_HOME%webappsROOT otFileFound.jsp

    (2)%CATALINA_HOME%webappsROOTWEB-INFweb.xml中web-app节点中添加和(1)中相同格式的内容,
    为了方便演示,此处使用404.html


    看看不存在时的效果:

    360浏览器对tomcat的配置的404有屏蔽作用:

    如果想让报404时直接跳转,则可以在404.html中增加一个跳转的动作即可:

    <html> 
    <head> 
    <meta http-equiv="Content-Language" content="zh-CN"> 
    <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312"> 
    <meta http-equiv="refresh" content="0.1;url=http://www.cnblogs.com"> 
    <title></title> 
    </head> 
    <body> 
    </body> 
    </html> 

    3.不仅可以根据html的错误代码来条转页面,也可以按异常类型来进行跳转,例如:

    <error-page>  
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/errorhandler.jsp</location>
    </error-page>

    不仅可以使用jsp内置exception对象来取得异常,也可以取得request中的attribute。例如:

    复制代码
    <%@page contentType="text/html;charset=Big5" isErrorPage="true"%>  
    <html>
    <head><title>错误信息</title></head>
    <body>
    错误码: <%=request.getAttribute("javax.servlet.error.status_code")%> <br>
    信息: <%=request.getAttribute("javax.servlet.error.message")%> <br>
    异常: <%=request.getAttribute("javax.servlet.error.exception_type")%> <br>
    </body>
    </html>

    http://www.cnblogs.com/yjhrem/articles/2206878.html

  • 相关阅读:
    设计模式(1)-行为类
    rocketmq(1)
    zookeeper(2)-curator
    Spring之HandlerInterceptor拦截器
    云之家如何获取登录用户信息?
    KDTable如何添加合计行?
    财务报表如何直接取数?
    DEP脚本
    Mybatis之关联查询及动态SQL
    Mybatis之XML、注解
  • 原文地址:https://www.cnblogs.com/softidea/p/5658827.html
Copyright © 2011-2022 走看看