Forums Register Login

Hopefully simple question

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
Hi,
One other way is to use java Spring API so that it will make ready your object before if needed.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
All interesting ideas. Thanks folks.

Regards,

Manny
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 940 times.
Similar Threads
Global Bean for Servlets/JSP
Tomcat servlet resource not available
Reading properties file
Methods for using JDBC Class in a web app?
Class not found errors for JSPs in sub-directories
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:38:23.