• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Init and Destroy method of servlet

 
Rahul Kumar Tiwari
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Parthiban Malayandi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Malayandi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha, Thanks for your clarification
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic