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 of servlets

 
kooskuntla venkat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we call destroy() method on servlets from service method?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But it wouldn't work that way. You shouldn't call that method, destroy method is called by container, instead.

Please refer to the docs.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember a post about this question being asked during an interview process... Only it was worded, "What will happen if you call the destroy() method from the service method?"

In short, the answer is the container will execute whatever logic is in the destroy() method, and then return flow back to the service method, just like it would with any other method call.

The point to remember here is that the destroy() method does not actually destroy the servlet as the name would imply. Instead, it is a place for you to put logic that should run when the container is preparing to destroy the servlet instance. For instance, if you needed to cleanup resources, this would be the place to do it. The container calls the destroy() method when the thread is no longer needed, and is about to be destroyed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic