Capturing all exceptions in Java EE web applications

First, I throw runtime exceptions for all fatal exceptions, so these exceptions are moved to the container where I currently use the error page (defined in web.xml). On this error page is the scriptlet that calls the registrar.

The problem I ran into is that the exception is no longer on the stack on this call. I have access to it from a query scope variable ("javax.servlet.error.message"). This line is a stack trace. I need this stack trace for logging purposes, and on different application servers, "javax.error_message" can be disabled for security reasons ........

So my question is how best to catch runtime exceptions from Java EE applications without wrapping everything in this:

try {} catch (Exception e) {logger.log(...)}

?

I want some way to call a logger from a container, perhaps right before the container catches the exception, for example.

+5
source share
4 answers

I have found a solution. After adding an answer filter and wrapping the .doFilter (req, resp) chain, do the following:

try {
    chain.doFilter(req,resp);
} catch (Exception e) {
    logger.error("", e);
    throw new RuntimeException(e);
}

It has been working fine so far and is not dependent on a specific infrastructure or application server.

+3
source

I don’t know anything in the Servlet API to do this.

Tomcat . context.xml, ,

<InstanceListener>myapp.MyListener</InstanceListener>

Tomcat InstanceEvent.AFTER_SERVICE_EVENT , , . .

+1

, , "exception" .. "exception" "" jsp. jsp.

0

All Articles