主題:Servlets和JSP,聲明異常處理錯誤頁面在web.xml中沒有顯示,儘管配置異常類型
我在web.xml中創建了以下錯誤聲明
<error-page>
<exception-type>exercises.MyException</exception-type>
<location>/WEB-INF/errors/error.jsp</location>
</error-page>
然後我創建MyException類
public class MyException extends Exception {
public static int JustThrowError(int n) throws MyException{
if(n<10)
throw new MyException();
else
return n;
}
}
爲了測試這個,我使用了一個簡單的jsp。
<body>
<%@ page import="exercises.*" %>
<%=MyException.JustThrowError(9) %>
</body>
我預計它顯示一個錯誤頁面(error.jsp文件)在我的web.xml中指定。相反,我得到如下所示的異常堆棧跟蹤。
任何人都可以解釋爲什麼沒有捕獲異常,並重定向到我的web.xml中定義的error.jsp?
SEVERE: Servlet.service() for servlet [jsp] in context with path [/my-struts-errors] threw exception [An exception occurred processing JSP page /forms/riskyPage.jsp at line 9
6: </head>
7: <body>
8: <%@ page import="exercises.*" %>
9: <%=MyException.JustThrowError(9) %>
10: </body>
11: </html>
Stacktrace:] with root cause
exercises.MyException
at exercises.MyException.JustThrowError(MyException.java:8)
at org.apache.jsp.forms.riskyPage_jsp._jspService(riskyPage_jsp.java:63)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
是的,org.apache.jasper.JasperException工程!但是,如果我想爲我的整個Web應用程序中發生的任何未處理的異常顯示錯誤頁面(error.jsp),該怎麼辦?這就是爲什麼我首先使用java.lang.Exception。如果事情出錯,我不希望客戶端看到堆棧跟蹤。 – justintime
在這種情況下,爲什麼不把java.lang.Exception(或Throwable)作爲web.xml中的異常類型? –