Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Init vs Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Init vs Constructor" Watch "Init vs Constructor" New topic
Author

Init vs Constructor

Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Hi,

I had searched a lot on this issue before posting a query regarding the same.
I can define a no-arg constructor in my servlet class & perform some initializations then why do i need to use the init() method .

I really didn't got any answer which pin-points to the issue .

Please throw more lighton the same ........

With Regards,
Mohit Agarwal.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

A simple answer is that the constructor is part of the Object lifecycle and init() is part of the Servlet lifecycle. There may be similarities but you can never be sure
Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Hi,

Thanx David for the response .
But the point is still not clear .

Mohit....
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Originally posted by Mohit Agarwal:
Hi,

I had searched a lot on this issue before posting a query regarding the same.
I can define a no-arg constructor in my servlet class & perform some initializations then why do i need to use the init() method .

I really didn't got any answer which pin-points to the issue .

Please throw more lighton the same ........

With Regards,
Mohit Agarwal.


All the servlet classes are implementations of javax.servlet.Servlet interface. The container initializes the servlet via the Init()method by passing the ServletConfig object. It is possible to do the same using a constructor accepting ServletConfig object as parameter but constructors are optional in a class and there is no way you can enforce it. In such a case it will know only at runtime that a constructur was not defined in the servlet config. This is my thinking.


Groovy
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
The constructor creates the object, while the init() method creates the servlet. Before the init() method is called (by the container), your servlet does not have access to the ServletContext or ServletConfig objects. Without them, you dont have access to things like init parameters or web app parameters.

Sure, technically you could create a no arg constructor and try and preform some initialization code in there, as long as you don't try and access any of the web app stuff, but why would you? There is nothing that cannot wait until the init() method().
[ August 01, 2005: Message edited by: Paul Bourdeaux ]

“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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Init vs Constructor
 
Similar Threads
Applets init()
why dose applets have init method and applications dont
Constructing objects - is there a difference?
EntityManager.find() returns null
what is the potential problem?