File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes destroy() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "destroy() method" Watch "destroy() method" New topic
Author

destroy() method

Ravissant Markenday
Ranch Hand

Joined: Nov 12, 2006
Posts: 47
Hi everyone !

Could anyone please tell me when exactly the destroy() method in the servlet lifecycle is called and by whom?

thanks in advance,
Ravissant Markenday
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It is called by the container.

There are several cases where the container can or will call this method.
The most common is when the servlet is being taken out of service.

SRV.2.3.4 End of Service
The servlet container is not required to keep a servlet loaded for any particular
period of time. A servlet instance may be kept active in a servlet container for a
period of milliseconds, for the lifetime of the servlet container (which could be a
number of days, months, or years), or any amount of time in between.
When the servlet container determines that a servlet should be removed from
service, it calls the destroy method of the Servlet interface to allow the servlet to
release any resources it is using and save any persistent state. For example, the
container may do this when it wants to conserve memory resources, or when it is
being shut down.
Before the servlet container calls the destroy method, it must allow any
threads that are currently running in the service method of the servlet to complete
execution, or exceed a server-defined time limit.




The best way to find out when the container may also call the destroy method is to download the servlet spec (link in my signature) and perform a search on the word "destroy".


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

Joined: Nov 12, 2006
Posts: 47
Thanks Ben !!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

When the entire server goes through a controlled shutdown, the destroy method will be called.

A more common scenario to think about though is the updating of a single application. An update usually involves taking various servlets out of service, which calls the destroy method, updating the application, and then loading the same servlets again. In this type of scenario, you must be careful to clean up resources, so when the second initialization happens, objects aren't needlessly duplicated, or conflicting.

Just a couple of examples. There are more.

-Cameron McKenzie
Ravissant Markenday
Ranch Hand

Joined: Nov 12, 2006
Posts: 47
Thanks Cameron!!
 
Consider Paul's rocket mass heater.
 
subject: destroy() method