Hi, This question is on servlet. Why cant we use constructor of a servlet class as a initializer for that servlet instead of init() method. Why we need init method when we can use non-argument constructor as initializer for that servlet. I know that init method has something to do with the life cycle of servlet. But still cant we use constructor instead of init method.
Thx , Steve
SCJP1.4<br />IBM Certified System Administrator -- WebSphere Application Server V5.0 (340)
1. I think it is the way you can seperate the initialization logic with basic default construction to make easy for the reusability.
2. The container will always instantiate the servlet with the default constructor and you won't get the servlet config object in the default constructor method, which is used to get the servlet configuration details, servletContext details.
With the above I can simply change the database connection string I need in the deployment descriptor when I change from one environment to another with no need to recompile anything. Way cool!
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
Gift giving made easy with the permaculture playing cards