• 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

usage of servlet init() method and servlet constructor

 
Ranch Hand
Posts: 548
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can anybody please tell me in what cases one uses servlets' init() method and servlets's constructor. Which one is preferred other in what scenarios ?

Thanks & Regards,
Kumar.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet container is responsible for creation of an instance of the servlet. As specified in the servlet API, the init method will be called (once only) before any request is processed. Any initialization that your servlet needs to have done before handling a request should be done from the init method.
(Note the parallel with the Applet API where the browser (applet container) is responsible for the creation of an instance, etc.)
Bill
 
Rr Kumaran
Ranch Hand
Posts: 548
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

But can you please answer to my specific question i.e. when you'll prefer to use servlet's constructor to servlet's init() method and vice versa.


Thanks & Regards,
Kumar.
 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravi,

The constructor is used when a call is made to the servlet by the client. It's init method is called by the container when it initially loads the servlet.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never use a servlet constructor and put everything in the init method.
Right off hand I can't think of any reason to, since you are guaranteed that the init method will be called after the servlet instance is created but before any request is handled.
Bill
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Duncan:
Ravi,

The constructor is used when a call is made to the servlet by the client. It's init method is called by the container when it initially loads the servlet.



But what about situation when all users use the same servlet??? In most
situations that is the case: one servlet many users!!!
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what about it?
I think you have not quite got the picture - here is the lifecycle of a servlet object;
1. servlet container is started
2. a request for servelet A comes in - but the container knows it does not have an instance of A
3. the servlet container creates an instance of A based on the web.xml contents - the ONLY instance
4. the servlet container calls the init method - the ONLY time this happens
5. the request is sent to the service method of A - usually translated to doGet or doPost - using a Thread managed by the servlet container
6. ANY NUMBER of other requests are sent to the service method of A (same single instance) each request with its own Thread
7. this continues for any amount of time
8. for some reason the servlet container wants to dump the instance out of memory - may because of an operator instuction, maybe just for the hell of it.
9. No more requests are accepted for A, the servlet engine calls the destroy method after the last request Thread is done.
10. back to 1
-------
So you see, programming for a servlet requires a certain change of viewpoint from programming for a desktop application.
Bill
 
asirob civokviz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I think I do understand servlet lifecycle!?

I quoted post in which was sad that servlet's
constructor is called when user accesses the page!
That is not true when there is only one instance
of servlet in container!!

Or am I wrong???
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic