Can we call destroy() method on servlets from service method?
Joined: Aug 15, 2004
Yes. But it wouldn't work that way. You shouldn't call that method, destroy method is called by container, instead.
Please refer to the docs.
Joined: May 24, 2004
I remember a post about this question being asked during an interview process... Only it was worded, "What will happen if you call the destroy() method from the service method?"
In short, the answer is the container will execute whatever logic is in the destroy() method, and then return flow 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. The container calls the destroy() method when the thread is no longer needed, and is about to be destroyed.
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook