Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

destroy method

 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic