Top Companies hiring on Shine.com. Click Here to apply !
Click Here to Submit Your Resume for MNC Jobs !
JavaKen.com

Go Back   JavaKen.com > J2ee Introduction > Servlets & Java Server Pages (JSP)
Forgot User Name / Password ?


Reply
 
Thread Tools Display Modes
  #1  
Old 03 December 2009, 09:36 PM
Nalan Nalan is offline
Junior Member
 
Join Date: 02 Jul 2009
Posts: 8
Default Can I call destroy() method inside the init() method?

Click Here To Register & Submit Your Resume !

Give me some example code....


Thanks In advance
nalan

Click Here To Register & Submit Your Resume !

Last edited by Nalan; 03 December 2009 at 09:39 PM.
Reply With Quote

  #2  
Old 06 December 2009, 10:57 PM
khadangap khadangap is offline
Member
 
Join Date: 21 Jun 2009
Location: Marathali,Bangalore
Posts: 66
Default Re: Can I call destroy() method inside the init() method?

HI Friend,
I have collected the answers of this Q and hope it will help you
What will happen if we call destroy() method from inside the


Whatever code is in the destroy method will be executed as soon as the servlet is loaded.init() method?


So will the servlet be destroyed and not initialized?
I was asked this question in an interview.

No. As Ben said, whatever code you put in the destroy() method will be called. That's all.

So will the servlet be destroyed and not initialized?
I was asked this question in an interview.


I've been wondering why that question gets asked so much.
I guess it would make a good interview question.
If that's the answer you gave, I'm assuming the interview didn't go very well.

There is a link to the servlet spec in my signature.
You might want to download it and read the section that covers the servlet life cycle. It's not a difficult read.

All of the life cycle methods init, destroy, doXxx, etc.. are callback methods. Instead of the spec providing these methods for you to call, you're supposed to implement the functionality of these methods for the container to call.

For instance, if you want something done when the servlet is initialized, you would override one of the init methods and add the code that you want executed in there. Likewise, if you want something to happen when the servlet is removed from service, you would add your code to the destroy method. If you don't override destroy, then nothing happens when destroy is called. You would never call destroy yourself. It is called by the container when the servlet is taken out of service.

So, to answer your question:
If you don't override destroy(), but added a call to it from the init method the container (when the servlet is put into service), will call your init method.
This will cause the default destroy method (which does nothing) to be called. Nothing will happen.
Then, when the servlet is taken out of service, the container will call the default destroy method. Again, nothing will happen.

If you override destroy then, when the servlet is put into service, the container will call the init method which (because you're calling destroy from init) will call the destroy method. When this happens, whatever code you put into destroy will be executed. Then, when the servlet is taken out of service, the container will call destroy and your code will be executed again.

Is their a way to explicit destroy the servlet from service method. As per discussion above, it seems if destroy() method is called from service or init method, it will just execute the code inside that method.But what if i really like to want to destroy the servlet instance based on some condition from service method.

Probably the safest and most universal way to take the servlet offline is to have one of your service methods throw an UnavailableException. This will ensure that it will not get any more requests and let the container gracefully handle any existing requests.


Your reply was
"If you don't override destroy(), but added a call to it from the init method the container (when the servlet is put into service), will call your init method.
This will cause the default destroy method (which does nothing) to be called. Nothing will happen.
Then, when the servlet is taken out of service, the container will call the default destroy method. Again, nothing will happen."

The statement in bold specifies that nothing will happen when the destroy method is called from init method.
The statement that has underlined specifies that nothing will happen when the container calls the destroy method.

So,does that mean that the servlet willl never be destroyed!!!???
I am totally confused. Please help me out.

You left out a crucial word when you quoted the underlined text: "default". The default destroy method does nothing; if it is overridden, then whatever code the overriding method contains will execute. No, the servlet will be destroyed after the container calls the destroy method. It's just that no user-specified action will happen, because there wasn't any.

Now please tell me if my understanding is correct. I have listed down my understanding here below.
1. If default destroy method is called from init method, nothing happens. The init process continues
2. If default destroy method is called from service method, nothing happens. The service process continues
3. Once step1/step2 is over and after servlets job is over, the container will call the destroy method and the servlet wil be removed from the container. This will happen once the servlet determines that the servlet is no more in use.
4. If destroy method is overridden and destroy is called from init, init process will continue even after executing the overridden destroy method - (In this case, is it possible to to override the destroy method so that the servlet is removed from the container?)
5. Once step 4 completes, remaining action is same as step 3
No. If your code calls the destroy method, then the servlet will not be taken out of service. That's what Ben meant by "Whatever code is in the destroy method will be executed as soon as the servlet is loaded." in his first post. If your code calls a method, then that method will be executed, and that's it. In particular, if your code calls the destroy method then the servlet will not be taken out of service.
Again - overriding or calling the destroy method does not lead to the servlet being taken out of service. The container does that when it sees fit to do so (and after it has called the destroy method). The servlet code can't cause the servlet to be taken out of service, unless it does what William mentioned.

I would simply take it as:
1. init() : A method that is executed as soon as the Servlet is alive.
2. doXXX() : A servlet is living and performing.
3. destroy() : A method that is executed just before the servlet dies.

This is better than confusing functionalities with names.
Reply With Quote
  #3  
Old 09 April 2010, 06:42 PM
chandrakanth.amuru chandrakanth.amuru is offline
Junior Member
 
Join Date: 15 Nov 2009
Location: Bangalore
Posts: 11
Default Re: Can I call destroy() method inside the init() method?

hi,
yes we can call the destroy() from init() but it's not recommended to call the destroy(). B'cause "typically" the code present in destoy() makes the variable servletConfig as unreferenced hence we can't use it in further code.
Reply With Quote
  #4  
Old 11 May 2010, 01:59 PM
chandrasekhardutta chandrasekhardutta is offline
Junior Member
 
Join Date: 08 May 2010
Posts: 6
Default Re: Can I call destroy() method inside the init() method?

hello boss

ca u please tell me from which init() do u want to call destroy() ?
there r two init methods available according to specification.

they are (1) first init()
(2) second init()

first init() will be executed by the web server. web server is not responsible to execute the second init(). first init() is responsible to call the second init().
So u can write destroy() as part of second init().

if any related queries u can send me on chandrasekhardutta@gmail.com

regards
shekar
Reply With Quote
  #5  
Old 14 May 2010, 07:35 PM
dkjena4u dkjena4u is offline
Junior Member
 
Join Date: 23 Aug 2008
Posts: 2
Default Re: Can I call destroy() method inside the init() method?

No we cannot call destroy() from init(). the serlet class will not compile. It will generate compile time error.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +5.5. The time now is 04:45 AM.


Powered by vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 2008 ChetanaS, All Rights Reserved.