aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference between init and servlet constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between init and servlet constructor" Watch "Difference between init and servlet constructor" New topic
Author

Difference between init and servlet constructor

georgy jacob
Ranch Hand

Joined: Nov 23, 2005
Posts: 53
Whats the purpose of the init method ? why didnt they just use the Servlet constructor for initialising purpose rather than using an init .
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
This is a frequently asked question in Javaranch. Search the Servlets forum. You can find many discussions there on this topic


Mani
Quaerendo Invenietis
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
This is actually one of my favorite questions because it is an extremely important difference, but one that a lot of people have a hard time grasping. I will give you a little bit of background on it, but then I encourage you to search the forums and read the discussions.

The constructor is called by the container simply to create a POJO (plain old java object). It is not really a servlet (or at least it cannot do any servlet like things) until it has been initialized. As a developer, you should never override the servlet's constructor.

The init method initializes the servlet, and gives it references to things like the servlet context. If you want something to happen when a servlet is initialized, override the no-arg init method and put it there.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
georgy jacob
Ranch Hand

Joined: Nov 23, 2005
Posts: 53
Thanks a lot for the information.Now i have understood it
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
As a developer, you should never override the servlet's constructor.



Whats wrong in overriding the servlet's constructor ? Anyways the super class' constructor would be called from our our constructor. Isnt it ?

Cheers,
Arvind
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between init and servlet constructor