1

I have a servlet which is loaded on server startup <load-on-startup>1</load-on-startup> in the init() method of the servlet I am checking for some parameter from a properties file. If the Parameter is not there, I want to stop the Entire Context from initializing.

public void init(){
   Readproperty pr = new ReadProperty();
     if(!pr.parameterExists()){
     //Edit: 
     throw new UnavailableException("Testing Stopping Context")

    }
}

What is the best way to do this? I do now want to Move this Code to my Context Listener Class so I am looking for the best way to do it from the `init()' method.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • You could throw a RuntimeException such as an IllegalStateException. – sp00m Jun 19 '14 at 07:41
  • I had thought of this too but could not analyse the implication or the defference with `javax.servlet.UnavailableException`. Is there no way of calling context destruction? – Stanley Mungai Jun 19 '14 at 07:43
  • 1
    Well, you're right, `init()` is allowed to throw a ServletException, so better throw an UnavailableException. – sp00m Jun 19 '14 at 07:47
  • Context is still starting see me edited Answer. – Stanley Mungai Jun 19 '14 at 08:37
  • And what if you throw an IllegalStateException? – sp00m Jun 19 '14 at 08:40
  • Same case the Context is Starting. – Stanley Mungai Jun 19 '14 at 08:51
  • I do not believe. Check again that you indeed throw the exception. Containers typically initialize webapp components sequentially and stop when any (?) exception is thrown. When spring context cannot be initialized it throws exception and context is not started. – AlexR Jun 19 '14 at 11:12

2 Answers2

4

You can't stop a web application from starting from a servlet.

If you want to stop the web application from starting move your test(s) to a ServletContextListener. An exception in the contextInitialized() method will stop the web application from starting.

If you are using a recent Tomcat 7.0.x or 8.0.x release you can use a Tomcat specific option failCtxIfServletStartFails on the Context or Host that will cause the web application to fail if any of the load on startup servlets fail but this is non-standard. The ServletContextListener is the better option.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
  • As I read Servlet Spec version 3.1 Section 11.6, it does *not* say the web app deployments halts when `ServletContextListener::contextInitialized` encounters an Exception. The spec only says obtusely “The container may respond to all subsequent requests to the Web application with an HTTP status code 500 to indicate an application error.” If halting is the intention of your JSR expert group, please clarify the language in upcoming Servlet 4 spec. This is a point of confusion; see [my list of similar SO questions](http://stackoverflow.com/a/37151007/642706). (Tomcat 8 halts, yes) – Basil Bourque May 11 '16 at 01:11
-3

Just throw any exception there. Initialization will be aborted.

AlexR
  • 114,158
  • 16
  • 130
  • 208