This week's book giveaways are in the iOS and Features new in Java 8 forums.
We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes how many times is destroy()called in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Servlets
Bookmark "how many times is destroy()called in servlet" Watch "how many times is destroy()called in servlet" New topic
Author

how many times is destroy()called in servlet

Ayaz uddin
Greenhorn

Joined: Jan 22, 2012
Posts: 6
how many times is destroy() called in servlet?
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

destroy() method is called :
1.) when on the whole, web application is undeployed from Servlet Container...
2.) When particular Servlet detail is deleted from web.xml...


SCJP 6 [SCJP - Old is Gold]
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Container calls only once , but you can call any number of time !
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2409
Arun Giridharan wrote:Container calls only once ...

That's generally true, but the container is free to take a servlet out of service and put it back into service at any time it chooses. So you can't rely on it only happening once.

...but you can call any number of time !

That's true, but app code should never call the lifecycle methods (like init and destroy); that's for the container to do.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62109
    
  68

Arun Giridharan wrote:Container calls only once , but you can call any number of time !

Repeated for emphasis: don't do this! Ever. Ever.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62109
    
  68

Ram Narayan.M wrote:2.) When particular Servlet detail is deleted from web.xml...

It has nothing at all to do with web.xml.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Tim Moores wrote:
Arun Giridharan wrote:Container calls only once ...

That's generally true, but the container is free to take a servlet out of service and put it back into service at any time it chooses. So you can't rely on it only happening once.

Can you explain on this , quite misleading.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62109
    
  68

Arun Giridharan wrote:
Tim Moores wrote:
Arun Giridharan wrote:Container calls only once ...

That's generally true, but the container is free to take a servlet out of service and put it back into service at any time it chooses. So you can't rely on it only happening once.

Can you explain on this , quite misleading.

It is not misleading at all. It is, in fact, quite accurate.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62109
    
  68

Essentially, it can happen up to once per servlet instance.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Bear Bibeault wrote:Essentially, it can happen up to once per servlet instance.

that's true.
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Really a nice explanation. Thanks Tim and Bibeault.


No pain, No gain.
OCJP 1.6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many times is destroy()called in servlet