1. 程式人生 > >Java Web 錯誤/異常處理頁面

Java Web 錯誤/異常處理頁面

<%@page pageEncoding="UTF-8" isErrorPage="true" import="java.io.*"%>  
<%!/**  
     * 收集錯誤資訊 輸出到網頁  
     *   
     * @param request  
     *            請求物件  
     */  
    public static OutputStream getError(HttpServletRequest request, Throwable ex) {  
        try(  
            OutputStream os = new ByteArrayOutputStream();// 建立一個空的位元組流,儲存錯誤資訊  
            PrintStream ps = new PrintStream(os);  
        ){  
            // 收集錯誤資訊  
            ps.println("錯誤程式碼: " +     request.getAttribute("javax.servlet.error.status_code"));   
            ps.println("異常 Servlet: " + request.getAttribute("javax.servlet.error.servlet_name"));  
            ps.println("出錯頁面地址: " + request.getAttribute("javax.servlet.error.request_uri"));  
            ps.println("訪問的路徑: " +   request.getAttribute("javax.servlet.forward.request_uri"));  
            ps.println();  
      
            for (String key : request.getParameterMap().keySet()) {  
                ps.println("請求中的 Parameter 包括:");  
                ps.println(key + "=" + request.getParameter(key));  
                ps.println();  
            }  
              
            for (Cookie cookie : request.getCookies()) {  
                ps.println("請求中的 Cookie 包括:");  
                ps.println(cookie.getName() + "=" + cookie.getValue());  
                ps.println();  
            }  
      
            // javax.servlet.jspException 等於 JSP 裡面的 exception 物件  
            if (ex != null) {   
                ps.println("堆疊資訊");  
                ex.printStackTrace(ps);  
                ps.println();  
            }  
  
            return os;   
        } catch (IOException e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
%>  
<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8" />  
    <title>錯誤頁面 code:${requestScope['javax.servlet.error.status_code']}</title>  
    <style>  
        body {  
            max-width: 600px;  
            min-width: 320px;  
            margin: 0 auto;  
            padding-top: 2%;  
        }  
        textarea {  
            width: 100%;  
            min-height: 300px;  
            outline:none;  
            border:1px solid gray;  
            padding:1%;  
        }  
        h1 {  
            text-align: right;  
            color: lightgray;  
        }  
        div {  
            margin-top: 1%;  
        }  
    </style>  
</head>  
<body>  
    <h1>抱 歉……</h1>  
    <div style="padding:2% 0;text-indent:2em;">尊敬的使用者:我們致力於提供更好的服務,但人算不如天算,有些錯誤發生了,希望是在控制的範圍內。如果問題重複出現,請向系統管理員反饋。</div>  
    <textarea><%  
     out.print(getError(request, exception));  
%></textarea>  
    <div align="center">  
            <a href="${pageContext.request.contextPath}">回首頁</a> | <a href="javascript:history.go(-1);">上一頁</a>  
    </div>  
</body>  
</html>  
------------------------------------------------------------------------------------------------------------------------------------

發生伺服器 500 異常,如果預設方式處理,則是將異常捕獲之後跳到 Tomcat 預設的異常頁面,如下圖所示。



不論哪個網站都是一樣的,所以為了滿足自定義的需要,Tomcat 也允許自定義樣式的。也就是在 web.xml 檔案中配置:

<error-page>  
    <error-code>500</error-code>  
    <location>/error.jsp</location>  
</error-page>  

首先說說自帶的邏輯。如果某個 JSP 頁面在執行的過程中出現了錯誤, 那麼 JSP 引擎會自動產生一個異常物件,如果這個 JSP 頁面指定了另一個 JSP 頁面為錯誤處理程式,那麼 JSP 引擎會將這個異常物件放入到 request 物件中,傳到錯誤處理程式中。如果大家有寫 Servlet 的印象,這是和那個轉向模版 JSP 的 javax.servlet.forward.request_uri 一個思路,保留了原請求的路徑而不是 JSP 頁面的那個路徑。在錯誤處理程式裡,因為 page 編譯指令的 isErrorPage 屬性的值被設為 true,那麼 JSP 引擎會自動宣告一個 exception 物件,這個 exception 物件從 request 物件所包含的 HTTP 引數中獲得。

request 物件中包含的異常資訊非常豐富,如下所示:

javax.servlet.error.status_code             型別為Integer        錯誤狀態程式碼
javax.servlet.error.exception_type          型別為Class          異常的型別
javax.servlet.error.message                 型別為String         異常的資訊
javax.servlet.error.exception               型別為Throwable      異常類
javax.servlet.error.request_uri             型別為String         異常出現的頁面
javax.servlet.error.servlet_name            型別為String         異常出現的servlet名
你可以用 Java 語句 request.getAttribute("javax.servlet.error.status_code") 獲取,也可以在 JSP 頁面中通過 EL 表示式來獲取,如 ${requestScope["javax.servlet.error.status_code"]}。

這個自定義錯誤頁面雖然簡單,JSP 本身也有很好的封裝結果,我也看過別人不少的資源,但細究之下也有不少“學問”,於是我想重新再”磨磨這個輪子“——首先 location 是一個 jsp 頁面,也可以是 servlet,不過萬一 servlet 也有可能啟動不起來的話那就使用簡單的 JSP 頁面就好了。我們通過 JSP 頁面定義內部類的方法,達到頁面與邏輯的分離(無須編寫 servlet)。其餘的思路如下:

  • 在 JSP 裡面完成 ErrorHandler 類,另有頁面呼叫這個 ErrorHandler 類
  • 不但可以接受 JSP 頁面的錯誤,也可接受 servlet 的控制器傳遞的錯誤,並且提取儘量多資訊
  • 全部內容先寫到記憶體,然後分別從兩個輸出流再輸出到頁面和檔案
  • 把錯誤資訊輸出到網頁的同時,簡單加幾句話,可以把網頁上的資訊也寫一份到資料庫或者文字
  • 可以返回 HTML/JSON/XML

實現程式碼如下:

/** 
 * 異常處理類 
*/  
class ErrorHandler {  
    // 全部內容先寫到記憶體,然後分別從兩個輸出流再輸出到頁面和檔案  
    private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
    private PrintStream printStream = new PrintStream(byteArrayOutputStream);  
  
    /** 
     * 收集錯誤資訊 
     * @param request 
     * @param exception 
     * @param out 
     */  
    public ErrorHandler(HttpServletRequest request, Throwable exception, JspWriter out) {  
        setRequest(request);  
        setException(exception);  
  
        if(out != null) {  
            try {  
                out.print(byteArrayOutputStream); // 輸出到網頁  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
          
         log(request);  
          
        if(byteArrayOutputStream != null)  
            try {  
                byteArrayOutputStream.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        if(printStream != null) printStream.close();  
    }  
  
    /** 
     *  
     * @param request 
     */  
    private void setRequest(HttpServletRequest request) {  
        printStream.println();  
        printStream.println("使用者賬號:" + request.getSession().getAttribute("userName"));  
        printStream.println("訪問的路徑: "   + getInfo(request, "javax.servlet.forward.request_uri", String.class));  
        printStream.println("出錯頁面地址: " + getInfo(request, "javax.servlet.error.request_uri", String.class));  
        printStream.println("錯誤程式碼: "     + getInfo(request, "javax.servlet.error.status_code", int.class));  
        printStream.println("異常的型別: "   + getInfo(request, "javax.servlet.error.exception_type", Class.class));  
        printStream.println("異常的資訊: "   + getInfo(request, "javax.servlet.error.message", String.class));  
        printStream.println("異常servlet: "  + getInfo(request, "javax.servlet.error.servlet_name", String.class));  
        printStream.println();  
          
        // 另外兩個物件  
        getInfo(request, "javax.servlet.jspException", Throwable.class);  
        getInfo(request, "javax.servlet.forward.jspException", Throwable.class);  
  
        Map<String, String[]> map = request.getParameterMap();  
  
        for (String key : map.keySet()) {  
            printStream.println("請求中的 Parameter 包括:");  
            printStream.println(key + "=" + request.getParameter(key));  
            printStream.println();  
        }  
          
        for (Cookie cookie : request.getCookies()){  // cookie.getValue()  
            printStream.println("請求中的 Cookie 包括:");  
            printStream.println(cookie.getName() + "=" + cookie.getValue());  
            printStream.println();  
        }  
  
    }  
  
    /** 
     *  
     * @param exception 
     */  
    private void setException(Throwable exception) {  
        if (exception != null) {  
            printStream.println("異常資訊");  
            printStream.println(exception.getClass() + " : " + exception.getMessage());  
            printStream.println();  
  
            printStream.println("堆疊資訊");  
            exception.printStackTrace(printStream);  
            printStream.println();  
        }  
    }  
  
        /** 
         *  
         * @param request 
         */  
        private void log(HttpServletRequest request) {  
            File dir = new File(request.getSession().getServletContext().getRealPath("/errorLog"));  
            if (!dir.exists()) {  
                dir.mkdir();  
            }  
              
            String timeStamp = new java.text.SimpleDateFormat("yyyyMMddhhmmssS").format(new Date());  
            File file = new File(dir.getAbsolutePath() + File.separatorChar + "error-" + timeStamp + ".txt");  
              
//              try(FileOutputStream fileOutputStream = new FileOutputStream(file);  
//                  PrintStream ps = new PrintStream(fileOutputStream)){// 寫到檔案  
//                  ps.print(byteArrayOutputStream);  
//              } catch (FileNotFoundException e) {  
//                  e.printStackTrace();  
//              } catch (IOException e) {  
//                  e.printStackTrace();  
//              } catch (Exception e){  
//                  e.printStackTrace();  
//              }  
        }  
  
        /** 
         *  
         * @param request 
         * @param key 
         * @param type 
         * @return 
         */  
        @SuppressWarnings("unchecked")  
        private <T> T getInfo(HttpServletRequest request, String key, Class<T> type){  
            Object obj = request.getAttribute(key);  
            return obj == null ? null : (T) obj;  
        }   
}  
這樣就可以完成異常的控制了。下面定義 web.xml,讓 tomcat 出錯引向我們剛才指定的頁面 error.jsp
<!-- 404 頁面不存在錯誤 -->  
<error-page>  
    <error-code>404</error-code>  
    <location>/WEB-INF/jsp/common/default/error.jsp</location>  
</error-page>  
<!-- // -->  
  
<!-- 500 伺服器內部錯誤 -->  
<error-page>  
    <error-code>500</error-code>  
    <location>/WEB-INF/jsp/common/default/error.jsp</location>  
</error-page>  
<!-- // -->  
我們安排一個預設的頁面如下


原始碼如下:

<%@page pageEncoding="UTF-8" isErrorPage="true"%>  
<%@ include file="/WEB-INF/jsp/common/ClassicJSP/util.jsp"%>  
<!DOCTYPE html>  
<html>  
<head>  
    <title>錯誤頁面</title>  
    <style>  
        body {  
            max-width: 600px;  
            min-width: 320px;  
            margin: 0 auto;  
            padding-top: 2%;  
        }  
          
        textarea {  
            width: 100%;  
            min-height: 300px;  
        }  
          
        h1 {  
            text-align: right;  
            color: lightgray;  
        }  
          
        div {  
            margin-top: 1%;  
        }  
    </style>  
</head>  
<body>  
    <h1>抱 歉!</h1>  
    <div style="padding:2% 0;text-indent:2em;">尊敬的使用者:我們致力於提供更好的服務,但人算不如天算,有些錯誤發生了,希望是在控制的範圍內……如果問題重複出現,請向系統管理員反饋。</div>  
    <textarea><%  
            new ErrorHandler(request, exception, out);  
           %></textarea>  
    <div>  
        <center>  
            <a href="${pageContext.request.contextPath}">回首頁</a> | <a href="javascript:history.go(-1);">上一頁</a>  
        </center>  
    </div>  
</body>  
</html>