你知道你可以設置一個「錯誤頁面」,但是你指的是什麼?如果它只是一個靜態網頁,那麼是的,你將無法訪問錯誤細節。但是,如果你把它轉發到正在處理錯誤由一個servlet,那麼你應該對你的錯誤的詳細信息,並且可以通過控制回球衣等
即
web.xml中:
<error-page>
<error-code>415</error-code>
<location>/InvalidContentHandler</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/InvalidContentHandler</location>
</error-page>
注:在上面的web.xml中,你應該用你所遇到的實際異常類型,你可以用「中的javax.servlet.error.exception」得到更換的java.lang.Throwable屬性,如下所示。
InvalidContentHandler.java:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processError(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processError(request, response);
}
private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException {
// Pass control to Jersey, or get some info:
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");
String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
...
}
來源
2014-10-17 16:17:06
Jon