This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Context Listener" Watch "Servlet Context Listener" New topic
Author

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:

1.
init()
{
Dog d;//Initialized to some Dog
super.init();
getServletContext().setAttribute(" dog ",d);

}


2.

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!!!


SCJP5 ,SCWCD5
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
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).


Ping & DNS - my free Android networking tools app
Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
Thanks dude got it now .
 
wood burning stoves
 
subject: Servlet Context Listener
 
Similar Threads
EL Execution Problem
jsp:useBean
Cannot get HttpSessionBindingListener method valueBound to trigger
please help with running this code
ServletContext