The most successful people in any walks of lives may not be the most talented ones but are definately the most passionate & dedicated ones.

Thursday, February 10, 2011

What happens if you call destroy() from init() in a servlet ?

Well the answer [believe me I was surprised to know this] is :-
destroy() gets executed and the initialization process continues.

Explanation:-In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed to and will not destroy a java servlet. Don’t get confused by the name. It should have been better, if it was named onDestroy().

The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues. With respective to this question, the destroy() gets executed and then the servlet initialization gets completed.

If you still aren't clear, then please re-visit servlet life cycle once.


Cheers !!!
Vinay

No comments:

Post a Comment