Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling destroy() from init()

 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made a servlet extending GenericServlet. My servlet contains init(), service() and destroy() methods.
From the init() method I make a call to destroy() method.

However on executing my servlet, the control still goes to service() method.

Pls clarify.

Pls see below my servlet class.

***************************************************************************

***************************************************************************



[BPSouther: Added code tags]
[ August 07, 2007: Message edited by: Ben Souther ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that a call to destroy() method will not destroy a servlet. If you are calling the destroy() method from init (or any other place for that matter), the destroy() method will execute like any other method and the control will return to the calling method once the destroy method is done with it's execution.
In other words, there is nothing special with the destroy method, apart from the fact that it will be called by the container automatically, when it is required.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, the destroy is called when the container wishes to take the servlet out of service.

but the converse is not true, that is if destroy is called by us the servlet is not taken out of service.

Is just a callback method that is called by the container to give us the last chance of cleaning up something before the servlet is taken out and garbage collected.

Hopes that answers your question.
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mani & Amol.

I've got the concept.
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for my knowledge, can you clarify which method actually takes the servlet out of memory. In other words where is the actual implementation of removing servlet from memory.
If it is in container, then will it be vendor specific? Can we customize it?
Also on what basis will the container decide to remove the servlet.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a servlet developer you have no control over this.

It is decided by the container when to remove the servlet instance from memory.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lalit Bansal:
Just for my knowledge, can you clarify which method actually takes the servlet out of memory. In other words where is the actual implementation of removing servlet from memory.
If it is in container, then will it be vendor specific? Can we customize it?
Also on what basis will the container decide to remove the servlet.


These things are left to the container developers.
We (application developers) shouldn't concern ourselves the the lower level inner workings of the container.
 
Lalit Bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for the clarification.

Request to moderator to please close the topic thread.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also...

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
UseRealWords

Abbreviations such as "u" or "ur" in
place of "you" and "you are" or "you're" confound language translation software making
it hard for our non-English speaking members to read your posts.
"plz" is not a word in the English language.

Again, welcome to JavaRanch and good luck with your question.
-Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic