主题:Servlet 和 JSP,声明式异常处理
我在 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>
我希望它显示在我的 web.xml 中指定的错误页面(error.jsp)。相反,我得到了异常堆栈跟踪,如下所示。
谁能解释为什么异常没有被捕获并重定向到我的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)