• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

init method and ServletContextListener

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We do override init() method if we want some initialization code to run before our servlet services any of the client requests.
We can also have some class implementing ServletContextListener interface listening for context initialization events so as to run some code which needs to run prior to any client request is fulfilled.
so I just wanted to know which is used when as both seem to be doing the same thing.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mahesh,

In simple terms class implementing ServletContextListener interface acts link a main method to the web application i.e. for example using a context parameter value you want to construct an object and set in context scope then this is the best one to use.

Servlet init method will be called when the container creates an instance of the servlet. So ideally here you should have servlet initialization code here.

Thanks,
Ram.
 
reply
    Bookmark Topic Watch Topic
  • New Topic