Preferred way to handle Java exceptions in ServletContextListener

前端 未结 4 1814
借酒劲吻你
借酒劲吻你 2020-12-29 06:18

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

For example,

public class Foo implements ServletContextL         


        
相关标签:
4条回答
  • 2020-12-29 06:40

    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.

    0 讨论(0)
  • 2020-12-29 06:40

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

    0 讨论(0)
  • 2020-12-29 06:45

    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.

    0 讨论(0)
  • 2020-12-29 07:02

    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.

    0 讨论(0)
提交回复
热议问题