• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContextListener

 
senthil rajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frnds,

Even it is simple doubt, pls clarify.

As per my understanding ServletContextListener will be used to create objects(like database connections), at the time of loading the application.

But why can't we write the object creation code inside the init() method?

Thanks,
Senthil.
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think you misunderstand. What you mean (create DB connection for exemple, when a servlet is initialized), you can write this in the init() method, no problem.

But ServletContextListener listens to the servlet CONTEXT lifecycle events, not the servlet. You can use this listener to create (and destroy) objects that may be used by many webapp components and you want to store in the context.

Don't mix up ServletContext and Servlet. Burn in your mind that ServletContext is really Application context.
 
senthil rajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frederic,

Crystal clear now.

Thanks again,
Senthil.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic