• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContextListener method calls

 
Mayank Mishra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) contextInitialized()called after the ServletContext is created.Am I correct?

2) contextDestroyed() called before the ServletContext becomes eligible for GC.Am I correct?

I guess it has to be that way else it will be analogous to this:
DOCTOR TO PATIENT--Take one pill in the night after sleeping and other before getting up in the morning.Or am I totally wrong and missing an entire scheme of the almighty container??

3)On page 164 para 4 of HFS&J authors mention "You can't really put that code inside..." but I conjured up an ugly way of doing that.See the snippet


......
String dburl=getServletContext().getInitParameter("DBConnectionURL");
Connection tomydb=getServletContext().getAttribute("DBCONN");
if(tomydb==null)
{
//code to make db conn and place it in the context
}
else
{
some other servlet already did that so just use it
}
......

I can place this code in init() of ALL my app's servlets who use DB and I am done.

PROS:Better if your company pays you by number of lines of code )
CONS:repetitive code in multiple servlets and if you forget to cut-paste-copy this even in one of the servlets using DB and by ur sheer misfortune it runs before others u r doomed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic