You can't make use of the constructor because the container calls it and therefore you can't pass any parameters to the constructor. Also at the point the constructor is called the class is not really a Servlet because it doesn't have a reference to the ServletConfig, which provides all the initialisation parameters etc.
Having said that if you really don't need the reference to ServletConfig and you really wanted to then I guess you could do some initialisation in the constructor, but bear in mind it is considered bad practice because that is what Sun supplied the init(ServletConfig) method for.
Joined: Jul 20, 2005
I should have also added that I think Bert and Kathy described this quite well in HFSJ at some point, probably at the point they talk about the Servlet Life Cycle.
Joined: Sep 02, 2005
I think I should start taking Tutions from U .......... :-)
But Mat I didnt got ur point saying You can't make use of the constructor because the container calls it and therefore you can't pass any parameters to the constructor
Why cant a container call constructor having parameters
Hi, As it is the container that manages a servlets lifecycle, you must define a generic way of working for all servlets. You can't use the constructor because otherwise you would have to modify the container to tell him to instanciate this particular servlet. Sebastien
Joined: Jul 20, 2005
Thanks for the complement As Sebastien said, the container can't pass parameters to the constructor, because you would need to change the container, that is rewrite parts of Tomcat etc, and how would the container know what parameters to pass to the constructor.
If someone was very clever and had the spare time, they could work out a way, maybe through the DD that the container could work out parameters for a constructor, and pass them in when the servlet was created. But that is so far away from the current servlet specification, and would be very difficult to implement in a generic fashion. Much better at this stage to just remember that the coonstructor needs to take no parameters.