通過URL訪問tomcat中資源,有許多常見的錯誤,比如說404呀,500呀什麼的,如果不加設定,瀏覽器頁面會直接顯示這些錯誤,使用者感受當然是相當不好的了,那麼如何解決這個問題呢。
其實很簡單,只需要在項目的web.xml中添加一些配置。
<error-page> <error-code>400</error-code> <location>/WEB-INF/views/error/400.jsp</location></error-page><error-page> <error-code>403</error-code> <location>/WEB-INF/views/error/403.jsp</location></error-page><error-page> <error-code>404</error-code> <location>/WEB-INF/views/error/404.jsp</location></error-page><error-page> <error-code>500</error-code> <location>/WEB-INF/views/error/500.jsp</location></error-page>
<!-- java.lang.Exception異常錯誤,依據這個標記可定義多個類似錯誤提示 --><!--<error-page>--> <!--<exception-type>java.lang.Exception</exception-type>--> <!--<location>/error.jsp</location>--><!--</error-page>-->
具體的如下:
Tomcat 的錯誤頁面是由 org.apache.catalina.valves.ErrorReportValve 類輸出來的。如果想自訂錯誤頁面,不需要修改該類。Servlet 規範聲明了相關的API,只需要在每個 web 應用的 web.xml 裡定義。可按照錯誤類型、錯誤碼配置。例如:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<error-page>
<error-code>404</error-code>
<location>/errorpages/404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorpages/exception.jsp</location>
</error-page>
</web-app>
注意錯誤頁面必須以“/”開頭,這樣任何path的404錯誤頁面及exception錯誤都會映射到這兩個檔案。然後在本web引用的errorpages下面放置404.jsp, exception.jsp兩個檔案。
錯誤頁面 404.jsp:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<html>
<header>
<title>404 page</title>
<body>
<pre>
<%
Enumeration<String> attributeNames = request.getAttributeNames();
while (attributeNames.hasMoreElements())
{
String attributeName = attributeNames.nextElement();
Object attribute = request.getAttribute(attributeName);
out.println("request.attribute['" + attributeName + "'] = " + attribute);
}
%>
</pre>
代碼中輸出了所有的 request 中的變數。從中也可以看到訪問哪個檔案出錯,跳到哪個錯誤頁面了,從而進行更詳細、更人性化的錯誤處理。例如,提示可能的正確網址等等。
例如:訪問一個不存在的頁面 page_not_exist.html,顯示的資訊為:
request.attribute['javax.servlet.forward.request_uri'] = /page_not_exists.html
request.attribute['javax.servlet.forward.context_path'] =
request.attribute['javax.servlet.forward.servlet_path'] = /page_not_exists.html
request.attribute['javax.servlet.forward.path_info'] = /errorpages/404.jsp
request.attribute['javax.servlet.error.message'] = /page_not_exists.html
request.attribute['javax.servlet.error.status_code'] = 404
request.attribute['javax.servlet.error.servlet_name'] = default
request.attribute['javax.servlet.error.request_uri'] = /page_not_exists.html
注意,該錯誤頁面必須大於512位元組,否則IE將不予顯示。因為IE預設只顯示大於512位元組的錯誤頁面。Firefox中正常顯示。可以添加一些其他資訊,將頁面大小擴充到512位元組以上。如果仍不能顯示,請檢查IE設定,將該選項選中。
異常處理頁面 exception.jsp:
<%@ page contentType="text/html; charset=UTF-8" isErrorPage="true" %>
<%@ page import="java.io.*" %>
<html>
<header>
<title>exception page</title>
<body>
<hr/>
<pre>
<%
response.getWriter().println("Exception: " + exception);
if(exception != null)
{
response.getWriter().println("<pre>");
exception.printStackTrace(response.getWriter());
response.getWriter().println("</pre>");
}
response.getWriter().println("<hr/>");
%>
注意isErrorPage熟悉必須為true,才能使用exception對象。exception即捕捉到的異常。此處可以對exception進行處理,比如記錄日誌、重新導向等等。這裡把exception trace列印出來了。
500、505 等錯誤頁面的處理類似於404。