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

usage of servlet init() method and servlet constructor

 
Rr Kumaran
Ranch Hand
Posts: 548
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • 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
 
asirob civokviz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • 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
  • 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???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic