Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes how many times is destroy()called in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply 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: 3
how many times is destroy() called in servlet?
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 244

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: 2407
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: 56554
    
  14

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

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


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56554
    
  14

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: 56554
    
  14

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: 56554
    
  14

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: 162

Really a nice explanation. Thanks Tim and Bibeault.


No pain, No gain.
OCJP 1.6
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how many times is destroy()called in servlet
 
Similar Threads
how many times is _jspService called?
who calls service method ?
destroying the servlet
how to make a servlet to run at startup of the application
finalize()