I have a JAR that suddenly needs to be re-packaged as a WAR (don’t ask why). It’s going to be deployed on Tomcat. I’d like to know what the bare minimum set up is (in terms of the web.xml and any other conf files) to instruct Tomcat to deploy the application and run a method that is normally called by the JAR’s main(String[]) method:
public class AppDriver {
public static void main(String[] args) {
AppDriver app = new AppDriver();
app.run();
}
// This is the method I need ran by Tomcat at deploy-time.
public void run() {
// ...
}
}
Since I’m not using Spring or any other kind of MVC framework, and since there are no servlets or need for CDI/contexts, I would imagine that my web.xml can be pretty simple; but surprisingly, I can’t find anything explaining exactly what I need.
I also imagine I need to annotate the the run() method somehow, or make reference to it in web.xml. But that’s where I’m stuck. Thanks in advance.
You can implement
ServletContextListener, in that case when tomcat deploys your application and starts it, yourcontextInializedmethod will be called, in that method you call your run method.Then in the web.xml you will add an entry for your class under
listenertag.Above approach is for servlet spec upto 2.5
If you are using Servlet Spec 3.0 you can simply add
@ServletContextListenerannotation to your class with no configuration required in web.xml, when your application is started, it will be invoked by your container.