Abort java webapp on startup

后端 未结 2 2073
灰色年华
灰色年华 2020-12-03 19:24

My webapp is part of a larger EAR that is deployed into a websphere server. The server hosts number of other apps on the same virtual server. My webapp has some initialisati

相关标签:
2条回答
  • 2020-12-03 19:59

    I'd recommend throwing a RuntimeException from ServletContextListener.contextInitialized.

    Servlet 2.3 wasn't very clear on this, but Servlet 2.4 added the following detail:

    Some exceptions do not occur under the call stack of another component in the application. An example of this is a … ServletContextListener that throws an unhandled exception during a notification of servlet context initialization…. In this case, the Developer has no opportunity to handle the exception. The container may respond to all subsequent requests to the Web application with an HTTP status code 500 to indicate an application error.

    Since it says that the servlet engine "may" disable access to application, you might find a server that does something else. However, Tomcat and WebLogic both disable the application, and the only other reasonable thing I can think of would be to ignore the exception. I can't see a container that did that being very popular—so you'd better test it in WebSphere yourself.

    0 讨论(0)
  • 2020-12-03 20:14

    Throwing a RuntimeException will probably make only that servlet unavailable. A safer way might be to implement something like a Spring interceptor that will forward to an error page or something if the checks didn't pan out. That way, you don't need to prevent the app from loading, but can handle it more gracefully at run time.

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