my dog learned polymorphism*
The moose likes Servlets and the fly likes use of init() in servlet life cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "use of init() in servlet life cycle" Watch "use of init() in servlet life cycle" New topic
Author

use of init() in servlet life cycle

Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Ranchers,

In the servlet life cycle, first the servlet is instantiated (the default constructor runs) and then the init() runs.
init() gives us a chance to initialize our servlet before handling any client requests.

Is it possible that we can write the default constructor, do the initialize the servlet (job of init()) inside the constructor?
In that case we will not need the init().

So the questions is what is the use of init() if I can do the initialization in the default constructor?

Thanks for your time and consideration.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41165
    
  45
Never, ever, use a constructor in a servlet. It's up to the servlet container to construct and destroy servlet instances at its discretion. That's why there is an init method - to allow the developer to execute code before the servlet is put into service.


Ping & DNS - my free Android networking tools app
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

No, you can not initialize your servlet using constructor. Yes you are right when servlet called the container calls first default constructor but it gives the instance of your servlet only object.

That object doesn't acquired any property of servlet. After called init() method which contains servlet config object and that gives to your object to servlet object.

So using default constructor you can not initialize your servlet.

you have to call init() method.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
steve claflin
Ranch Hand

Joined: Dec 04, 2008
Posts: 54
As an explanation of why it is this way, it is allowable for the container to serialize the servlet object at shutdown, and reinstate if from storage at the next startup. Thus the constructor wouldn't be run again. The init method will be called, however, after the object is deserialized.
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Ulf, Nishan and Steve,

Thanks for clearing my doubt.

To summarize,
after the constructor runs, a normal java object is created.
but only after the init method runs, the java object gets a servlet config from the container, thus transforming it from a normal java object to a servlet object.
It also makes sense that container creates and destroys the instances of servlet. so it is not wise to write the default constructor.

Thanks Steve for the insight about the servlet object serialization.

Thanks again for taking the time.

Best Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

"Matt Spiffy", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: use of init() in servlet life cycle
 
Similar Threads
init() method
Why does a servlet has init method
we can use constructor to initialize the parameters
Why cann't we initialize servlets in constructor?
Servlet Basic Question.