aspose file tools*
The moose likes Servlets and the fly likes what happend,when we call destroy() in service() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "what happend,when we call destroy() in service() method?" Watch "what happend,when we call destroy() in service() method?" New topic
Author

what happend,when we call destroy() in service() method?

pcreddy narra
Greenhorn

Joined: Jun 01, 2006
Posts: 1
what happend,when we call destroy(), in service() method?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

when you call the destroy method, the destroy method gets called. Is this what you expected?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Maybe you wanted to know does this cause the servlet to be unloaded or similar? No it does not. The confusion here is in cause and effect - the servlet going out of service causes the destroy() method to be called and gives you achance to clean up resources. Calling the destroy method does not cause it to be unloaded.

Dave
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Dont worry , the Servlet won't die.


Groovy
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In simple terms:
The destroy method is not there for you to call.
It's there for the servlet container to call.

If you want something to happen when your servlet is destroyed (logging, cleaning up open file handlesn, etc...), put some code in the destroy method and the container will run it when the servlet is destroyed. Mostly, this happens when the app is stopped or reloaded.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
I have feeling that servlet specification 2.6 should include methods to unload servlet. I notice that question about destroy is so frequently asked. From other side probably destroy needs to be renamed to something more clue giving, like onPreUnload().


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I have feeling that servlet specification 2.6 should include methods to unload servlet.
But why would a servlet author want to unload a servlet themselves when the entire life cycle is maintained by the container? Wouldn't this leave the container a bit puzzled about what to do with requests coming to that servlet?
From other side probably destroy needs to be renamed to something more clue giving, like onPreUnload().
Note the use of the @PreDestroy annotation now available in Java EE 5 which replaces the need for an explicit destroy() method (although that's still in the interface contract). You could therefore have a method:


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Originally posted by dema rogatkin:
I have feeling that servlet specification 2.6 should include methods to unload servlet.


Why? You can easily take a servlet out of service by throwing an UnavailableException.
[ June 01, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Thanks, it's helpful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what happend,when we call destroy() in service() method?