and*_*Pat 3 java web.xml servlets custom-error-pages
我有一个用servlet和JSP开发的web应用程序.IllegalArgumentException如果我插入错误的参数,我配置我的应用程序抛出一个.然后我以这种方式配置了我的web.xml文件:
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
当我上升404 error,然后它工作和呼叫error.jsp,但当我上升java.lang.IllegalArgumentException,然后它不起作用,我有一个blank page而不是error.jsp.为什么?
服务器是Glassfish,日志显示真的IllegalArgumentException上升.
你不应该抓住并压制它,但要放手吧.
即不做:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
doSomethingWhichMayThrowException();
} catch (IllegalArgumentException e) {
e.printStackTrace(); // Or something else which totally suppresses the exception.
}
}
Run Code Online (Sandbox Code Playgroud)
而是放手吧:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doSomethingWhichMayThrowException();
}
Run Code Online (Sandbox Code Playgroud)
或者,如果你真的想要捕获它用于记录等(我宁愿使用过滤器,但是ala),然后重新抛出它:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
doSomethingWhichMayThrowException();
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果它不是运行时异常,然后重新抛出它包装ServletException,它将被容器自动解包:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
doSomethingWhichMayThrowException();
} catch (NotARuntimeException e) {
throw new ServletException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12439 次 |
| 最近记录: |