• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call destroy() inside init()

 
amna vijay
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen if we call destroy() method from inside the init() method?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
amna vijay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So will the servlet be destroyed and not initialized?

I was asked this question in an interview.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the answer..got the point
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bk Jacky
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

Thanks Ben you have given a very good explanation for destory() method.
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
danny blow
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) It's meant for you to override and supply any shutdown activities.

2) The code gets executed. Period.
 
nipun yadvinder
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Himanshu Kansal

Very simple and to the point explaination.
Thanks

And Thanks all on the forum ..


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic