File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "use of init() in servlet life cycle" Watch "use of init() in servlet life cycle" New topic

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

Joined: Mar 22, 2005
Posts: 42965
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.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689


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

Joined: Jan 10, 2002
Posts: 63838

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

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: use of init() in servlet life cycle
jQuery in Action, 3rd edition