• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Run a class or method when Tomcat starts?

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know how I can do this? I need to fill some Hashtables when the Tomcat server is started, and only then. Can anyone help me figure out how I can run a method or class when this happens? Thanks.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure there are multiple ways to do this, but one suggestion off the top of my head would be to have a servlet be instantiated when tomcat starts up. As long as you are not implementing the SingleThreadModel tomcat should not load new instances of a servlet. When declaring your servlet in your xml file be sure to include your load on startup tag in the servlet definition like this....

This will cause the servlet to be instantiated and loaded into memory when tomcat starts. Therefore you could include some functionality in the servlet's init() method that could load data into a hashtable.
If you are implementing the SingleThreadModel than you will have to be careful doing this since the Tomcat Container can instantiate multiple servlets of the same class.
Hope this helps.
Cheers,
Ryan
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way would be to write a bean that implements a ServletContextListener. When you context is loaded by tomcat (when it starts) it will create an instance of your bean and call this method:

You must have an entry like the following in your deployment descriptor:
 
Ryan Bailey
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Point Jason,
Now that I have my SCWCD I would of used the same idea Better if going to be implementing the SingleThreadModel.
Cheers,
Ryan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic