aspose file tools*
The moose likes Servlets and the fly likes Call destroy() inside init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Call destroy() inside init()" Watch "Call destroy() inside init()" New topic
Author

Call destroy() inside init()

amna vijay
Greenhorn

Joined: May 09, 2008
Posts: 4
What will happen if we call destroy() method from inside the init() method?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Whatever code is in the destroy method will be executed as soon as the servlet is loaded.

Why would you want to do something like that?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
amna vijay
Greenhorn

Joined: May 09, 2008
Posts: 4
So will the servlet be destroyed and not initialized?

I was asked this question in an interview.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

Originally posted by amna amna:
So will the servlet be destroyed and not initialized?

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by amna amna:
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.

Does this help?
[ May 09, 2008: Message edited by: Ben Souther ]
amna vijay
Greenhorn

Joined: May 09, 2008
Posts: 4
Thanks a lot for the answer..got the point
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9315
    
109

Originally posted by Ben Souther:


I've been wondering why that question gets asked so much.
I guess it would make a good interview question.



Added this question to our FAQ What happens if i call destroy() from init()?

[ UD: Changed the FAQ URL. There was already a section on lifecycle questions in the FAQ; I've merged the new entry into it. ]
[ May 12, 2008: Message edited by: Ulf Dittmer ]

[My Blog] [JavaRanch Journal]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Bear Bibeault made a good point about this in an off-line conversation.
A lot of the confusion around the life servlet cycle methods would have been eliminated if their names started with "on" such as onDestroy, onInit, etc..

The names of these methods won't change but it's probably a good idea, when learning about the servlet life cycle, to think of these methods spelled this way.
Nassu Ali
Greenhorn

Joined: Nov 10, 2004
Posts: 3
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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
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.

Bill


Java Resources at www.wbrogden.com
Bk Jacky
Ranch Hand

Joined: Jun 11, 2005
Posts: 74
Hi Ben,

Thanks Ben you have given a very good explanation for destory() method.


SCJP1.4<br />SCWCD1.5<br />"Nothing is impossible"
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 92
Hi Ben,

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.

Regards,
Sriram
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
The statement that has underlined specifies that nothing will happen when the container calls the destroy method.

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.

So, does that mean that the servlet will never be destroyed?

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.


Ping & DNS - updated with new look and Ping home screen widget
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 92
Great. That explanation was nice :-)
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

Regards,
Sriram
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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.

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.

4. If destroy method is overridden and destroy is called from init, init process will continue even after executing the overridden destroy method

Yes.

(In this case, is it possible to to override the destroy method so that the servlet is removed from the container?)

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.
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 92
Thanks a lot for those wonderful clarification :-)
I was out of Ranch for few days now. Hence the delay in reply.
Thanks again.

Regards,
Sriram
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Many doubts cleared I guess.

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.

Regards


Experience and talent are independent of age
danny blow
Greenhorn

Joined: Aug 31, 2009
Posts: 1
Hi, Im new to this forum...I have gone thru all the replies ..but still i do have couple of questions ...can any one please explain...


1) what happens when the container calls the default destroy method ?
if nothing happens ..then what is for its in use? or the servlet never gets destroyed ? or it only meant to be overidden??

2) what happens when i myself calls the default destroy method ?

(Note: Im not at all overidding the destory methods!)


DannyBlow
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

1) It's meant for you to override and supply any shutdown activities.

2) The code gets executed. Period.
nipun yadvinder
Greenhorn

Joined: Dec 05, 2007
Posts: 2
The destroy() method is there to finish any activities that you might want to do before the servlet gets destroyed.

So, when container knows that it wants to destroy a servlet it calls destroy() method of that servlet.
Container manage the lifecycle of servlet through a wrapper.

So if we call destroy() within the code its same as calling any other method as it is container which destroys the servlet not the destroy() method of servlet.

Regards,
Nipun
S Jothimani
Greenhorn

Joined: Jun 29, 2011
Posts: 9
Hi,

From the replies so far,
can we conclude that, container will call the default destroy() method before destroying the servlet, so the actual servlet destroyness was handled by some other part of container ?
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 44

@Himanshu Kansal

Very simple and to the point explaination.
Thanks

And Thanks all on the forum ..


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call destroy() inside init()
 
Similar Threads
destroy() method of servlet.
what happens if i call destroy() method in init() or service() method ?
Call destroy() method inside the doGet() or doPost() Methods
destroy() method
init and destroy method