Tuesday, September 27, 2005

What will happen if I call destroy method from service method?


Calling destroy() method from service [ doGet (or) doPost ] method is just like calling another method . There will be no effect on the life cycle of the servlet until destroy is called by the Container. Means destroy() will work like any other ordinary method till it is called by Container for shutting down the servlet.

The container will execute whatever logic is in the destroy() method, and then return the control back to the service method, just like it would with any other method call.

The point to remember here is that the destroy() method does not actually destroy the servlet as the name would imply. Instead, it is a place for you to put logic that should run when the container is preparing to destroy the servlet instance. For instance, if you needed to cleanup resources, this would be the place to do it.

Note:

You should not call the life-cycle methods yourself. They are meant to be called by the container. The one who calls the servlet's destroy is the Container NOT the programmer. When the Container did so, then the servlet is available (eligible) for garbage collection.

0 Comments:

Post a Comment

<< Home