aspose file tools*
The moose likes Servlets and the fly likes Init and Destroy method of servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Init and Destroy method of servlet" Watch "Init and Destroy method of servlet" New topic
Author

Init and Destroy method of servlet

Rahul Kumar Tiwari
Greenhorn

Joined: Jun 09, 2008
Posts: 14
Sir,

For experiment purposes I am calling distroy() method within the init() method but servlet service() method is running why ? Can any body tell me why ? because i have read that destroy() mehtod of servlet run at the end of servlet life cycle and only once.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

because i have read that destroy() mehtod of servlet run at the end of servlet life cycle and only once.

That's Correct.
For experiment purposes I am calling distroy() method within the init() method but servlet service() method is running why ?

Those are container callback methods (i.e: supposed to be called by the container when it decides to remove the servlet instance from service). So calling it in your code is just like calling a normal method.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi Rahual,

You are calling destroy() inside init but to use that, you are invoking servlet which is a REQUEST to a Web Container . so what happens is in order to respond to your request the servlet will be initalised(init) and service () method is invoked to do that. so the point is WHENEVER you are hitting a servlet means it is request, so service() method will be invoked to process your request .

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42644
    
  65
Parthiban Mahiby wrote:You are calling destroy() inside init but to use that, you are invoking servlet which is a REQUEST to a Web Container . so what happens is in order to respond to your request the servlet will be initalised(init) and service () method is invoked to do that.

No, this is at least misleading, if not downright wrong. No request needs to be made in order for the init method to be called. For example, the servlet may be listed as load-on-startup, in which case it will be initialized at web app startup time, not at request time. And even if it's not load-on-startup, the container may decide to init the servlet anyway (although that's not how the common containers work). Plus, the container is free to destroy and re-init servlets at any time it choose to do so (although, that, again, is not how common containers work).

The point is, the servlet spec says only that the servlet will be initialized before requests are sent to it, and that destroy() will be called after the last request has been handled - it does not say when, exactly, or how often, either of these events will occur.


Ping & DNS - my free Android networking tools app
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi Ulf,

Thanks for correcting my mistake and helped me to understood exactly what happens.

I understood that init() mehod not necessarily will be called just before service() method .

Rahul Kumar Tiwari wrote: I am calling distroy() method within the init() method but servlet service() method is running why ?


For Rahul the service method is called because he explicitly invoked a servlet(which is a request to a web container).
what I understood is correct ?

please clarify me.

Once again thanks for correcting my mistake.

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Parthiban Mahiby wrote:For Rahul the service method is called because he explicitly invoked a servlet(which is a request to a web container).
what I understood is correct ?

Correct.
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Vijitha, Thanks for your clarification
 
 
subject: Init and Destroy method of servlet