jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes init method and ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "init method and ServletContextListener" Watch "init method and ServletContextListener" New topic

init method and ServletContextListener

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
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.

I am slow but sure
Ram Charan

Joined: Jun 07, 2008
Posts: 16

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.

I agree. Here's the link: http://aspose.com/file-tools
subject: init method and ServletContextListener
It's not a secret anymore!