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.
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.
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: 35239
7
posted
0
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.
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 ?