This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
Joined: Sep 23, 2010
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 ?
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.