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

  • 相关阅读:
    上传几张智能开关产品图片
    python+ueditor+七牛云存储整合
    Shell脚本检查memcache进程并自己主动重新启动
    Cocos2dx 3.x创建Layer的步骤
    HDU 5009 Paint Pearls (动态规划)
    (转)Spring4.2.5+Hibernate4.3.11+Struts2.3.24整合开发
    (转)Spring提供的CharacterEncoding和OpenSessionInView功能
    (转)为Spring集成的Hibernate配置二级缓存
    (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案二
    (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • 原文地址:https://www.cnblogs.com/softidea/p/5658827.html
Copyright © 2011-2022 走看看