This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet destroy() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet destroy() method" Watch "Servlet destroy() method" New topic
Author

Servlet destroy() method

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

I have some queries regarding the destroy() method of the servlet life cycle:

a) Who calls the destroy() method during the final phase of the servlet life cycle? Does the container do it like it does the initialization of the servlet class when it is first loaded ? Or is it handled internally in some other way?

b ) When is actually a servlet class destroyed and available for GC ?


~ Mansukh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
a) only the servlet container should call destroy as part of the life cycle.
It is up to the servlet programmer to code the destroy method to clean up any resources used. Be careful, check for null ref before trying to - for example - close database connections.
b) 1. say servlet class instance, not servlet class - using the right words helps understanding
....2. actual cleanup is up to the servlet container and jvm

Java Resources at www.wbrogden.com
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

William Brogden wrote:a) only the servlet container should call destroy as part of the life cycle.
It is up to the servlet programmer to code the destroy method to clean up any resources used. Be careful, check for null ref before trying to - for example - close database connections.
b) 1. say servlet class instance, not servlet class - using the right words helps understanding
....2. actual cleanup is up to the servlet container and jvm


OK. So, as far as the init() and destroy() methods are concerned, it is up to the servlet container and the JVM do decide when to initialize and destroy the servlet instance. Correct?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

It's up to the servlet container to decide that.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Thanks Paul. Will close this topic now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet destroy() method
 
Similar Threads
When Servlet's delete() method is called?
Destroy() method in the life cycle of the servlet
when the destroy() methid caled
why lifecycle method required ?
how many times init() method gets called