My requirement is to run a java program as soon as my web app loads on the tomcat server. What is the best way to do it?
I was thinking of calling the method inside the init() method of the servlet, but if i am correct that might run the program during the application startup or might run it when the first request hits. So how do ensure that the method is called when the application is loaded on the server.
You may also recommend me a better way of doing so.
Once upon a time, it was, in fact common practice to use an init() method for that purpose. You simply marked the servlet as "load on startup".
However, these days, the recommended procedure is to use a ContextListener to invoke your run-on-startup code.
Some people, when well-known sources tell them that fire will burn them, don't put their hands in the fire.
Some people, being skeptical, will put their hands in the fire, get burned, and learn not to put their hands in the fire.
And some people, believing that they know better than well-known sources, will claim it's a lie, put their hands in the fire, and continue to scream it's a lie even as their hands burn down to charred stumps.
Does this tiny ad smell okay to you?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop