22

For servlet lifecycle stuff, what do you guys recommend doing in response to an exception...

For example,

public class Foo implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {
           // something nasty
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        try {
           // something nasty
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

I'm not entirely sure what will handle the runtime exception above. I'm working from the idea that if exceptions are thrown here, they're serious enough to break the system completely so an (unhandled) runtime exception may be ok.

I guess I'm asking what handles unchecked exceptions from servlet context listeners?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Toby
  • 9,523
  • 8
  • 36
  • 59

4 Answers4

11

When you catch an exception, you might want to consider setting a ServletContext attribute to indicate that an error has been encountered. That way, if the container has not disabled the app, you can have Filters and/or Servlets inspect the ServletContext attribute and take appropriate action, like displaying an error page.

kschneid
  • 5,626
  • 23
  • 31
  • 3
    Glassfish 3.0.1 refuses to deploy a web application, if ServletContextListener.contextInitialized throws a RuntimeException. However, if the application has already been deployed, and a RuntimeException is thrown when the server is restarted, GF 3.0.1 refuses to start at all (which makes it impossible to undeploy using the supplied tools). In this case we opted to set a ServletContext attribute in the listener, and output error message in a filter. – Vetle Nov 16 '10 at 13:09
7

It seems that ServletContentListener is not designed to be able to exercise control over the lifecycle (otherwise it would be allowed to throw a ServletException).

As such, I would not rely on a RuntimeException to do anything useful. Looking at some other threads here, it seems to be logged and ignored on certain application servers.

If it is critical that the application does not start when your code fails, you should move that code into a Servlet's initialization section.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Servlet spec says that the app server "may" abort starting the application: http://stackoverflow.com/questions/272194/abort-java-webapp-on-startup/272747#272747 – Thilo Oct 27 '10 at 09:02
0

It is good tone to show some page like "Technical error, sorry" and not to show the stacktrace with the error message. Just log it and forward the user to error page.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • 3
    The methods in the servlet context listener will not be executed on a HTTP request but on context startup, so there's no way to show an error page. – Philipp Jardas Oct 27 '10 at 23:10
-2

We can the Exception through the object.printStackTrace(); or calling the Exception through out.print("Exception is"+ ex);