This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run a program on application startup in tomcat?

 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

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.

Thanks!
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18014
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic