aspose file tools
The moose likes Servlets and the fly likes Hopefully simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Hopefully simple question" Watch "Hopefully simple question" New topic
Author

Hopefully simple question

Manny Garcia
Greenhorn

Joined: Apr 04, 2007
Posts: 28
I have a large-ish web app with a lot of moving parts. In particular I have a lot of cached data that doesn't get initialized until the first time a class is instantiated. My question is: Is there a way to set up a class with a method that will be called when the web app is started so that I can ensure that my cache is loaded before the first time someone wants to use it and perform any other start up tasks I'd like as well?

Regards,

Manny
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19672
    
  10

And here I thought you had a question about web services!

If you want something to be done when your web application starts, just create a class which implements ServletContextListener and put your code into the contextInitialized method. Configure the name of that class into your web.xml in the appropriate place.
Manny Garcia
Greenhorn

Joined: Apr 04, 2007
Posts: 28
Holy cow Paul, that was easy! Thank you.

Regards,

Manny

Paul Clapham wrote:And here I thought you had a question about web services!

If you want something to be done when your web application starts, just create a class which implements ServletContextListener and put your code into the contextInitialized method. Configure the name of that class into your web.xml in the appropriate place.
abani patra
Ranch Hand

Joined: Oct 11, 2011
Posts: 70
Hi,
One other way is to use java Spring API so that it will make ready your object before if needed.
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 171

Hi,
There is another simply way to initiate a servlet by giving positive numbers for <load-on-startup></load-on-startup> in web.xml for particular servlet which one needs to initiated when the web application starts.

No pain, No gain.
OCJP 1.6, Liferay Certified Developer 6.1
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63039
    
  69

Kumaravadivel Subramani wrote:
There is another simply way to initiate a servlet by giving positive numbers for <load-on-startup></load-on-startup> in web.xml for particular servlet which one needs to initiated when the web application starts.

That's a hack from the days before context listeners were added to the Servlet API. It's a poor practice to use servlets for application initialization in modern web apps.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Manny Garcia
Greenhorn

Joined: Apr 04, 2007
Posts: 28
All interesting ideas. Thanks folks.

Regards,

Manny
 
Consider Paul's rocket mass heater.
 
subject: Hopefully simple question
 
It's not a secret anymore!