File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why init ( ) why not Constructor ??  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why init ( ) why not Constructor ??  " Watch "Why init ( ) why not Constructor ??  " New topic
Author

Why init ( ) why not Constructor ??

Abhishek Jain
Greenhorn

Joined: Sep 02, 2005
Posts: 26
Why do we have init method ..........
After all why can't we make use of servlet constructor for initilization ....


Regards
Abhishek


SCJP <br />SCWCD <br />SCBCD <br />SCEA Preparing
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
Good question , Why init() why not construtor ??


SCWCD 1.4, SCJP 1.4
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi,

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.

HTH

Mat
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
HI,

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.

Mat
Abhishek Jain
Greenhorn

Joined: Sep 02, 2005
Posts: 26
Thankx Mat

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

Regards
Abhishek
Sebastien Col
Greenhorn

Joined: Aug 18, 2005
Posts: 13
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
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi Abhishek,

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.

Mat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why init ( ) why not Constructor ??
 
Similar Threads
Constructor
Why Init Method, why can't we use constructor instead?
init and constructor in servlet
Why Init() method
Why using init() method