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
posted 7 years ago
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.