This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes destroy() method effect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "destroy() method effect" Watch "destroy() method effect" New topic
Author

destroy() method effect

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 423
Hi, Please advice how/when do we see the effect of destroy() method..What I mean is, How do we know/make sure that destroy() method is executed during servlet's lifetime.


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1294

Hi Harikrishna,

the Servlet lifecycle methods are automatically called by the runtime environment, i.e. Servlet container. The container implements the Servlet API and guarantees that it calls such lifecycle methods when appropriate according to the specification.

You can just override these methods to get something like callback methods which are then called by the container. But you don't have to worry when or how the methods are called.

Marco
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 423
Thank You Marc. I agree with you. Becuase destroy() is a callback method and called by container when the servlet is about to be GCed. So, What is the use of defining destroy() method in the Servlet ?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1294

So, What is the use of defining destroy() method in the Servlet ?

Well, it makes only sens to override destroy() when you think there is something to clean up when a Servlet instance is going to die. That depends very much on the Servlet in question. Usually the garbage collector takes care of in-memory garbage but a Servlet may also use other resources like database connections which could be something you want to clean up inside the destroy() method.

After all it's optional to override the destroy() method and most of the time it may be even better if there is nothing to clean up explicitly.

Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: destroy() method effect
 
It's not a secret anymore!