Can some code run when we first deploy a WAR file?
Is there any method or API that I can use so that whenever I deploy a new WAR file, a part of code should execute or perhaps when Tomcat starts, the respective servlet should start or run some code continuously. Reviving an old question since the only answer doesn't show any example. In order to run a custom piece of code whenever a web application WAR is deployed/undeployed or Tomcat is started/stopped, you need to: Implement the ServletContextListener listener and its methods contextInitialized() and contextDestroyed() . Let Tomcat know about your implementation. According to the