| Author |
destroy() method effect
|
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
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: 1221
|
|
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: 422
|
|
|
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: 1221
|
|
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
|
 |
 |
|
|
subject: destroy() method effect
|
|
|