• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Init and Destroy method of servlet

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijitha, Thanks for your clarification
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic