permaculture playing cards
The moose likes Servlets and the fly likes Servlet Context Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Context Listener" Watch "Servlet Context Listener" New topic

Servlet Context Listener

Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
Hi I have a query .I want to make an object as soon as the servlet's servlet context gets initialized.I think of two ways:

Dog d;//Initialized to some Dog
getServletContext().setAttribute(" dog ",d);



class MyListener extends ServletContextListener
contextInitialized(ServletContextEvent e)
//Initialized to some Dog
e.getServletContext().setAttribute("dog",d) ;

Will both work??
Which is better and why ???

Please helpppppppppp!!!

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Assuming that the init is the one in the Servlet interface, then that will not work. A servlet's init method is not generally called upon the servlet context's initialization. It is called whenever the servlet gets initialized (which will be some time, possibly a long time, later).
Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
Thanks dude got it now .
I agree. Here's the link:
subject: Servlet Context Listener
It's not a secret anymore!