當我們訪問tomcat的一個不存在的頁面,返回錯誤信息如下:
這樣的界面直接暴露給用戶並不友好,有時候還不安全,因此一般需要修改默認的錯誤頁。
vim /$TOMCAT_HOME/conf/web.xml
在文件末尾,</web-app>的前面,加上以下內容:
<error-page> <error-code>400</error-code> <location>/error.html</location> </error-page> <error-page> <error-code>404</error-code> <location>/error.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/error.html</location> </error-page>
例:
在/$TOMCAT_HOME/webapps/ROOT目錄下添加error.html文件(該頁面可以替換成你自己自定義的界面):
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head> <title>網頁訪問不了</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="404/error_all.css?t=201303212934"> </head> <body class="error-404"> <div id="doc_main"> <section class="bd clearfix"> <div class="module-error"> <div class="error-main clearfix"> <div class="label"></div> <div class="info"> <h3 class="title">啊哦,你所訪問的頁面不存在了,可能是炸了</h3> <div class="reason"> <p>可能的原因:</p> <p>1.手抖打錯了。</p> <p>2.鏈接過了保質期。</p> </div> </div> </div> </div> </section> </div> </body></html>
再次打開一個不存在的界面,顯示如下: