Why do we need a constructor for a Servlet class when we can use init method? I know constructors are called before the init method, but not able to figure out how initialising the variables in constructors help than doing the same in init method. Any info on this will be of great help.
Thanks and Regards
Normally you would never write a constructor for a servlet.
But as a servlet is a class like any other it will have one.
when u r in a servlet class constructor it is just like writing a constructor in a normal java .
During the init mothod of the servlet you servlet object can use the services on the container
I was feeling unusually verbose. So I blogged on this...http://23c.blogspot.com/2005/08/techie-sideslip.html
First, it is not advisable to create a constructor in a Servlet other than the default.
Second, the servlet container is managing the life-cycle of these servlet instances. Variables initialized inside the default constructor of a servlet will only be run once during servlet instantiation. Since servlet instance are actually reused, the constructor is not invoked anymore during reuse, but the init method is always called once the servlet is reused from the pool.