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.


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.


Blogger Andrea Peterson said...

Pajamas Media ramping up
Pajamas Media has scheduled Nov. 16 as launch day in New York for its right-leaning blog news service and advertising combine.
Find out how you can buy & sell anything, like music on interest free credit and pay back whenever you want.

2:27 AM  

Post a Comment

<< Home