File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 42929
    
  68
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: http://aspose.com/file-tools
 
subject: Servlet Context Listener