It's not a secret anymore!
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "destroy() method " Watch "destroy() method " New topic

destroy() method

tilak kumar

Joined: Feb 16, 2005
Posts: 15
Can we call destroy() method forcefully ?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

destroy() doesn't actually destroy anything.
It's just a place for you to put code that you want called before a servlet is destroyed.
The container calls it for you.

What is it you're trying to do?

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

Joined: Feb 20, 2001
Posts: 580
The destroy() method will be called for you at the appropriate time by the container.

James Carman, President<br />Carman Consulting, Inc.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by tilak kumar:
Can we call destroy() method forcefully ?

Yes. Check it out.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
Servlet "lifecycle" methods like init and destroy should never be called by the programmer. See the discussion in javax.servlet.Servlet JavaDocs.
If you think your servlet architecture requires such a thing, you better go back to the drawing board.
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
There was a requirement for starting some server first time a servlet is called and it was done in init method. It gave no problems and is running fine till today. Also in the destroy method, the stop server method is called which will stop the server(in case the server has not stopped earlier).
These were done in tomcat and is working fine. So now I dont understand what is problem in using init and destroy. I referred to servlet javadocs and I couldnt find anything mentioned as not to use the init and destroy methods in code.
Can you please throw more light on why these cant be used?

Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
I am very sorry for being confused. I think you meant that one should not call the init and destroy method in the code. But there is no harm in using them(which means the container will call init and destroy when required).
Sorry again for being confused.

Sandeep Saha

Joined: Mar 28, 2005
Posts: 5
I do aggree with william. Destroy is a method that a user can call. But it is not recomanded. Destroy method is used when we want to do something at the end of the servlet lifecycle and is called by the container.So avoid calling destroy method always.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
why do you want to call the destroy() method anyway?
I agree. Here's the link:
subject: destroy() method
It's not a secret anymore!