Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

doubts about importance of ServletContextListener  RSS feed

 
Rachit Kumar Saxena
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!!! I read in HeadFirst Servlets and JSP that ServletContextListener is important to my app as it helps me initialise my app before it can service the client.Why not write that sort of code in init method that would do the job.for example instead of overriding methods from the listener we could work straightaway in init() method.Please help me to get the concept.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet's init method is specific to that one servlet and should be used only for setup that pertains to that servlet. The context listener should be used to initialize application-wide concerns.

Sure, you could use a servlet's init lifecycle method to init app-wide concerns*, but there are lots of thing that you can do that are poor practices. This is one of them.






* And in older versions of the servlet spec, that was all that was available. Not anymore.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!