Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init method and ServletContextListener

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • 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.
 
Ram Charan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic