This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hopefully simple question

 
Manny Garcia
Greenhorn
Posts: 28
  • 0
  • 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
Pie
Posts: 20156
23
MySQL Database
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All interesting ideas. Thanks folks.

Regards,

Manny
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic