|
|
The Life Cycle of a Servlet |
The
destroymethod provided by theHttpServletclass destroys the servlet and logs the destruction. To destroy any resources specific to your servlet, override thedestroymethod. Thedestroymethod should undo any initialization work and synchronize persistent state with the current in-memory state.The following example shows the
destroymethod that accompanies theinitmethod in the previous lesson:public class BookDBServlet extends GenericServlet { private BookstoreDB books; ... // the init method public void destroy() { // Allow the database to be garbage collected books = null; } }A server calls the
destroymethod after all service calls have been completed, or a server-specific number of seconds have passed, whichever comes first. If your servlet handles any long-running operations,servicemethods might still be running when the server calls thedestroymethod. You are responsible for making sure those threads complete. The next lesson shows you how.The
destroymethod shown above expects all client interactions to be completed when thedestroymethod is called, because the servlet has no long-running operations.
|
|
The Life Cycle of a Servlet |