• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading some values during the application load  RSS feed

 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I want to cache some simple database query results when the application is deployed and store it in the context scope. Normally, i would do it in the init() method of the command servlet. But i don't have the source code for the servlet. So, what i was thinking was make a Listener class that implements the ServletContextListener and store the values in there (in the ContextInitialized method). I can define that Listener in my deployment descriptor file. Am i on right track??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I do something similar frequently.

In fact, it is a better idea to use the context listener as the listener is guaranteed to have been executed before any servlet loads.
 
Vicky Pandya
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are not on right track. you should extend the servlet(which you don't have source code for) in extended servlet's init call super.init() and along with initialize and values to put into context.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are on the right track. That is exactly what the ServletContextListener is designed for.

A ServletContextListener's job to to initialize the application or prepare it for shutdown.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!