如何在 Java servlet Web 应用程序中捕获未捕获的异常

发布于 2024-12-04 05:37:14 字数 278 浏览 0 评论 0原文

是否有一种标准方法来捕获 java servlet 容器(如 tomcat 或 Jetty)内发生的未捕获的异常?我们运行许多来自库的 servlet,因此我们无法轻松地将我们的 try/catch 代码放入其中。如果能够以尽可能通用的方式捕获我们的 Web 应用程序(在 Jetty 中运行)中所有未捕获的异常,并通过提供的 API 将其记录到我们的错误跟踪器中,那就太好了。

请不要,我只需要记录异常,无论重定向是否问题到自定义错误页面都对我没有帮助。我们通过 GWT-RPC 完成所有操作,因此用户永远不会看到错误页面。

Is there a standard way to catch uncaught exceptions that happen inside of a java servlet container like tomcat or Jetty? We run a lot of servlets that come from libraries so we cannot easily put our on try/catch code. It would also be nice to in as generic of a way as possible catch and log all uncaught exceptions in our web application (which runs in Jetty) to our bug tracker via the API provided.

Please not I need to log the exceptions only, whether a a redirect is issues to a custom error page will not help me. We do everything via GWT-RPC so the user would never see an error page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-12-11 05:37:14

我认为自定义过滤器实际上效果最好。

@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);
        }
    }
}

I think a custom filter actually works best.

@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);
        }
    }
}
不乱于心 2024-12-11 05:37:14

web.xml (部署描述符)中,您可以使用 元素通过异常类型或 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>

对于以 NetBeans 为中心的描述,请继续阅读配置 Web 应用程序:映射错误错误屏幕(Java EE 6 教程)(或参阅Java EE 5 教程版本)。

In web.xml (the deployment descriptor) you can use the <error-page> element to specify error pages by exception type or HTTP response status code. For example:

<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>

For a NetBeans-centric description, mosey on over to Configuring Web Applications: Mapping Errors to Error Screens (The Java EE 6 Tutorial) (or see the Java EE 5 Tutorial's version).

握住你手 2024-12-11 05:37:14

不能 100% 确定这是否适用于 servlet 容器,或者此调用需要向上游走多远,但您可以调用 static setDefaultUncaughtExceptionHandler 方法在 Thread 上设置一个处理程序来处理所有未捕获的异常。

Not 100% sure if this will work with a servlet container, or how far upstream this call would need to go, but you can call the static setDefaultUncaughtExceptionHandler method on Thread to set a handler that will handle all uncaught exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文