First of all, you should never ever ever ever call System.exit()
from within a servlet container, as there might be other applications running within the same process as yours, and it would be incredibly incorrect to forcibly kill the entire process.
Assuming your "init servlet" implements ServletContextListener
, there is no formal way defined in the API/interface to signal to the servlet container "please shut down the app neatly". You could however throw some sort of RuntimeException
from the contextInitialized()
method, which in most containers (at least Tomcat) will stop the startup of your webapp and leave it in the "stopped" state. However I doubt that Tomcat will continue to call your shutdown listeners.
You might want to rethink your design so that your critical data / shutdown logic is not tied so tightly with the lifecycle of the servlet container.