my dog learned polymorphism*
The moose likes Servlets and the fly likes destroy method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "destroy method" Watch "destroy method" New topic
Author

destroy method

Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
Hello Experts!
Two basic questions:
1. Can we override/overload destroy method of a servlet?
2. What will happen if we give a call to the destroy method from the service method of a servlet?
Regards,
Anupreet
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Of course, we can override/overload the destroy method, so that this method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet´┐Żs current state in memory.
But I'm not sure about your second question, but I hope we can call destroy method from service method...For example, we may want a servlet to be destroyed, when there is an error occurred or some other reasons... But I'm not quite sure about it... Just my opinion...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

You can certainly call the destroy method from other methods of the servlet. It will execute the code present in the destroy method. This will not destroy the servlet instance.
The destroy is supposed to be called by the container. When it calls the servlet will be ready for retirement.
[ November 13, 2003: Message edited by: Pradeep Bhat ]

Groovy
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Pradeep, you got reasonabe explanation... It is not usual to call destroy method from other methods, it's supposed to be called by the container instead...
Anupreet, can I ask you why you want to deal with the destroy method, since it will be handled by the container itself?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

There are certain contract that the servlet developer has to follow when writing servlets. So dont break the contract by calling the destroy method.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Pradeep Bhat:
There are certain contract that the servlet developer has to follow when writing servlets. So dont break the contract by calling the destroy method.

Sure Pradeep, that's the question that I want to ask him... I'm wondering why he wants to break such contract between servlet developer and the container...
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
ko ko and pradeep!!!Thanks
This is a Interview question......what will happen if destroy called from the service method???
normally people will say it will throw exception,it will destroy and so on....This is a good information.


Thanks,
Mathews
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: destroy method
 
Similar Threads
Call destroy() inside init()
destroy method of servlet
frnds come up with solution
calling destroy in init method
can we call destroy method in servlet service method?