Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of init() in servlet life cycle

 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
steve claflin
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Matt Spiffy", please check your private messages for an important administrative matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic