Tomcat中配置自定义404错误页面

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

如果%CATALINA_HOME%\conf\web.xml和具体应用中都有设置%CATALINA_HOME%\webapps\ROOT\WEB-INF\web.xml

则应用中的生效。

 

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

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


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

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


效果展示:
正常情况:

 

出现404时的场景:

%CATALINA_HOME%\webapps\ROOT\notFileFound.jsp

 

(2)%CATALINA_HOME%\webapps\ROOT\WEB-INF\web.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

 

posted @ 2016-07-10 23:00  沧海一滴  阅读(18542)  评论(0编辑  收藏  举报