What happens when we call destroy() method in init() method in servlets

Showing Answers 1 - 13 of 13 Answers

Praveen Stella

  • Jan 5th, 2007

init() & destroy() are two different method in a servlet.Calling destroy() inside a init() is not possible and it doesn't make sense.

  Was this answer useful?  Yes

Methods init() and destroy() are two separate methods meant for some specialized handling of things like closing a connection pool, freeing the memory used by local variables etc.When the servlet container is about to unload the servlet - it will call the destroy() method where the user may have put some cleanup code for his application.Now, coming back to your question, it is "very much possible" to call destroy() inside the init() but it would have no special effect - it will be similar to calling any other method within the init().

  Was this answer useful?  Yes

Its nothing like you can kill the child as soon as it takes its birth. Basically destroy() will be called by the container, when the servlet instance is getting destroyed.
So even if destroy method is called explicity from init() or service() nothing happens, it wont make any sense.

  Was this answer useful?  Yes


  • Jan 28th, 2008

It does not make any affect, because destroy method only call by the container when they want to unload the servlet or jsp. By callin destry() within the init() it will call the destroy method, it ll run the code within the destroy() and return back to init(), it does not mean that it will stop the execution process.

  Was this answer useful?  Yes

It does not effect, If U call init() inside destory() method.Coz these are life cycle methods, so container only call these methods....If u written  init() inside destor() but destoy() method can not works.

  Was this answer useful?  Yes


  • Jan 20th, 2010

The destroy method can be called inside the init method. It will execute the code written in the destroy method, and flow will be back to the init method and continues.

Now the question arises....  since the destroy method is overridden so in the run time it will call the our servlet class method . So will it unload the servlet?

Yes it will unload the servlet. since init(),service() and destroy() methods are the servlet life cycle methods and these methods are called by container itself. So First this will call the overridden destroy method then container will call its destroy method.

Eg. This is the same scenarios happen in the EJB too. As call back methods are overridded. It tell the container first do the things which are written in the callback method then execute their lifecycle method.


  • Feb 16th, 2010

We can call a destroy() method from init() or service() normally like any other methods. Container will executes the destroy() method but does not unloads the servlet object from the memory. The unloading of servlet can be taken care by container only.

  Was this answer useful?  Yes

Ashwani Gupta

  • Apr 29th, 2016

overriding init() method wont do any special effect here since web containers initializes the servlets by calling init() method with ServletConfig object as parameter and not the init() method without any argument. ServletConfig contains all the parameters defined in web.xml which are used to initialze a servlet by a web container

  Was this answer useful?  Yes


  • May 9th, 2017

Nothing Special. It will just execute normal methods. But, it wont work like life cycle method. When JVm invokes these methods then only it will execute like life cycle method.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions