web開發中異常信息是很重的信息,對開發人員是其至關重要的,對這些異常信息進行轉換爲用戶能理解的信息就更重要了~html
在單純的JSP開發中,處理異常信息通常使用web.xml來定義。java
02 |
<error-code> 400 </error-code> |
03 |
<location>/ 400 .html</location> |
07 |
<error-code> 404 </error-code> |
08 |
<location>/ 404 .html</location> |
12 |
<error-code> 500 </error-code> |
13 |
<location>/error.jsp</location> |
這是很簡單的!web
若是如今想在頁面中設置一個隱藏div來供開發人員查看異常信息呢?服務器
整理下網站說的一些方法:jsp
最常說的:工具
01 |
<% @page contentType= "text/html;charset=Big5" isErrorPage= "true" %> |
03 |
<head><title>出現錯誤</title></head> |
05 |
<H1>錯誤:</H1><%=exception%> |
08 |
exception.printStackTrace(response.getWriter()); |
由於這個頁面調用了exception內置對象,因此isErrorPage必須爲true。網站
這個是能打印出異常信息的,可是放入了response中,頁面從頭就開始打印異常信息,用戶不明白異常信息~影響用戶使用。ui
另外一種常見方法:spa
不只可使用jsp內置exception對象來取得異常,也能夠取得request中的attribute.net
1 |
<% @page contentType= "text/html;charset=Big5" isErrorPage= "true" %> |
3 |
<head><title>錯誤信息</title></head> |
5 |
錯誤碼: <%=request.getAttribute( "javax.servlet.error.status_code" )%> <br> |
6 |
信息: <%=request.getAttribute( "javax.servlet.error.message" )%> <br> |
7 |
異常: <%=request.getAttribute( "javax.servlet.error.exception_type" )%> <br> |
同理的還有
<%= exception.getMessage()%>
<%=exception%>
<c:out value="${requestScope['javax.servlet.error.message']}"/>
這個也可能打印異常信息,但有時只會打印出一個null.沒有任何有價值信息。
-----------------------
還有一個特殊狀況:
Error Page在IE下不能轉發的問題
這是IE自身的設定致使的,通過百度,找到幾個解決辦法:
1, IE設定 工具-->Internet選項-->高級--->顯示http友好錯誤信息(取消選擇) , 這樣就能夠了
2, 設置指定錯誤頁頁狀態爲正常,來告訴IE這不是一個服務器錯誤, 從而不顯示IE的自定義錯誤頁
<%
response.setStatus(200); // 200 = HttpServletResponse.SC_OK
%>
3, 把錯誤頁作大一點,弄個幾百K 就能夠顯示錯誤頁面 (加一個div塊,display設爲none就能夠了),這個問題比較奇怪.
這個問題我尚未遇到過~先記錄在這兒吧~~
如今能符合要求的處理方法是:
01 |
<%@ page language= "java" contentType= "text/html; charset=GB18030" |
02 |
pageEncoding= "GB18030" %> |
04 |
<%@ page isErrorPage= "true" %> //必定要寫,不能顯示錯誤 |
06 |
response.setStatus(HttpServletResponse.SC_OK); //這句也必定要寫,否則IE不會跳轉到該頁面 |
07 |
String path=request.getContextPath(); |
12 |
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" > |
13 |
<title>Insert title here</title> |
17 |
<div>系統執行發生錯誤,信息描述以下:</div> |
18 |
<div>錯誤狀態代碼是:${pageContext.errorData.statusCode}</div> |
19 |
<div>錯誤發生頁面是:${pageContext.errorData.requestURI}</div> |
20 |
<div>錯誤信息:${pageContext.exception}</div> |
23 |
<c:forEach var= "trace" items= "${pageContext.exception.stackTrace}" > |