hi Generally constructors are for initializing instance variables. but in servlets we are using Life cycle method init(ServletConfig sc) for getting initialization parameters.As servlet is instantiated by container is this the reason we are not writing our own constructor in servlet? if not what is the reason please explain if we pass our own constructor what will be the containers and servlet reaction thanks in advance saiprasanna
If your servlet class has a constructor with parameters, it will never be called by the servlet container.
If your servlet class has a public arg-less constructor, it will be called.
Generally speaking, instance variables one might use in a servlet are related to the servlet environment - for example initialization parameters read from web.xml. Therefore they should be initialized in init as per the API.
Incorrect use of servlet instance variables is the cause of many beginner problems. Bill
subject: constructors are for initializing instance variables