First of all, you should never ever call System.exit() from the servlet container, as there may be other applications running in the same process as yours, and it would be incredibly incorrect to forcefully kill the whole process.
Assuming that your "init servlet" implements ServletContextListener , there is no formal signal defined in the API / interface to signal the servlet container "please shut down the application carefully." You could, however, throw a RuntimeException from the contextInitialized() method, which in most containers (at least Tomcat) will stop your webapp from starting and leave it in a stopped state. However, I doubt that Tomcat will continue to call shutdown listeners.
You might want to rethink your design so that your critical data / shutdown logic is not closely related to the servlet container life cycle.
source share