This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hopefully simple question

 
Manny Garcia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manny Garcia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All interesting ideas. Thanks folks.

Regards,

Manny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic