File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Context Listener

 
Abhijit Rai
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks dude got it now .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic