This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes calling destroy() from init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "calling destroy() from init()" Watch "calling destroy() from init()" New topic
Author

calling destroy() from init()

Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
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

Joined: Mar 11, 2002
Posts: 1140
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.


Mani
Quaerendo Invenietis
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
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

Joined: Jan 29, 2007
Posts: 63
Thanks Mani & Amol.

I've got the concept.
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
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

Joined: Oct 26, 2006
Posts: 218
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Thanks to all of you for the clarification.

Request to moderator to please close the topic thread.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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
 
 
subject: calling destroy() from init()
 
Similar Threads
servlet doubt
Confused about Life cycle of Servlets
Not able to connect to Mysql through servlet
Tomcat 4.0 problem with Filter class
Servlet program not running