| Author |
usage of servlet init() method and servlet constructor
|
Rr Kumaran
Ranch Hand
Joined: Sep 17, 2001
Posts: 548
|
|
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.
|
RR Kumaran
SCJP 1.4
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12269
|
|
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
|
Java Resources at www.wbrogden.com
|
 |
Rr Kumaran
Ranch Hand
Joined: Sep 17, 2001
Posts: 548
|
|
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
Joined: Nov 01, 2002
Posts: 363
|
|
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.
|
No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12269
|
|
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
Joined: Jun 12, 2004
Posts: 24
|
|
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
Joined: Mar 22, 2000
Posts: 12269
|
|
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
Joined: Jun 12, 2004
Posts: 24
|
|
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???
|
 |
 |
|
|
subject: usage of servlet init() method and servlet constructor
|
|
|