GeekInterview.com
Series: Subject: Topic:
Question: 50 of 177

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

Asked by: Interview Candidate | Asked on: Dec 26th, 2006
Showing Answers 1 - 11 of 11 Answers
Praveen Stella

Answered On : 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.

  
Login to rate this answer.

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().

  
Login to rate this answer.

Yes it does not make sense but its pretty much possible. Check my reply to the question for details.

  
Login to rate this answer.
sunil.yadav

Answered On : Jan 31st, 2007

View all answers by sunil.yadav

Its very simple, you are killing a child as soon as its takes birth, and it could not serve any thing.

  
Login to rate this answer.

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.

  
Login to rate this answer.
achyut

Answered On : Jan 28th, 2008

View all answers by achyut

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.

  
Login to rate this answer.
manjeet dewda

Answered On : Feb 8th, 2008

View all answers by manjeet dewda

I think the best answer is the Eclipse....just override init() mehtod and try to call destroy() in it..i think this will work...

  
Login to rate this answer.
sampra

Answered On : Feb 13th, 2008

View all answers by sampra

Yes it does not make sense but its pretty much possible

  
Login to rate this answer.
Karuna Reddy

Answered On : Jul 4th, 2008

View all answers by Karuna Reddy

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.

  
Login to rate this answer.
pankaj4mhp

Answered On : Jan 20th, 2010

View all answers by pankaj4mhp

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
ravi_1229

Answered On : Feb 16th, 2010

View all answers by ravi_1229

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.