This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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.