ben*_*rre 14 java exception-handling jetty uncaught-exception
是否有一种标准的方法来捕获在诸如tomcat或Jetty之类的java servlet容器内发生的未捕获的异常?我们运行了很多来自库的servlet,所以我们不能轻易地把我们放在try/catch代码上.通过提供的API将我们的Web应用程序(在Jetty中运行)中的所有未捕获的异常捕获并记录到我们的错误跟踪器中,这也是很好的方式.
请不要我只需要记录例外,无论重定向是自定义错误页面的问题都无济于事.我们通过GWT-RPC完成所有工作,因此用户永远不会看到错误页面.
ben*_*rre 13
我认为自定义过滤器实际上效果最好.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
} catch (Throwable e) {
doCustomErrorLogging(e);
if (e instanceof IOException) {
throw (IOException) e;
} else if (e instanceof ServletException) {
throw (ServletException) e;
} else if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
//This should never be hit
throw new RuntimeException("Unexpected Exception", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*all 11
在web.xml(部署描述符)中,您可以使用该<error-page>元素通过异常类型或HTTP响应状态代码指定错误页面.例如:
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<exception-type>com.example.PebkacException</exception-type>
<location>/error/UserError.html</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
对于以NetBeans为中心的描述,请参阅配置Web应用程序:将错误映射到错误屏幕(Java EE 6教程)(或参阅Java EE 5教程的版本).