• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

use of init() in servlet life cycle

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Matt Spiffy", please check your private messages for an important administrative matter.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic