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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM