• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What happens when you call destroy() on a servlet???

 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What happens when you call destroy() on a servlet instance? I think, container kills all the thread instance running on the servlet, is it correct?.
In one of the jwebplus question explanation is given as -- "The servlet container ensures that all the requests that are being serviced are completed. ", is that mean container waits, till all the threads finishes their job?


Thanks,
 
Murali Mohan A
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----------------------------------------------------------------------
"The servlet container ensures that all the requests that are being serviced are completed. ", is that mean container waits, till all the threads finishes their job?
----------------------------------------------------------------------

Yes,exactly the container waits for all the threads are finished and then destroys the servlet instance which cannot be used for service after that.


Murali
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been discussed a plenty of times. Make a simple search here
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, container kills all the thread instance running on the servlet, is it correct?.

No, this is not correct. Calling destroy() on a servlet does not destroy the servlet. It is simply a placeholder method that you can override if you want some logic to be run befoe the container destroys the servlet. Do a search in this forum and you will find many threads discussing this. hth
 
rehans oberoi
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok paul u tell what will happen
after overriding destroy method
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. First, let's look at what the API actually says about the destroy() method.
public void destroy()

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

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.
The most important line to note is that it is "Called by the servlet container to indicate to a servlet that the servlet is being taken out of service". It is does not contain the logic that actually takes the servlet out of service. This is where most get confused.

If I were to override the destroy method with the following code:then when the servlet is about to be destroyed (for instance, when the server is shutting down), I would see the following printed to my standard output: "This servlet is being destroyed...".

Did this help, or would you like a more practical example?
 
rehans oberoi
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi paul

it is more than enough
u are exactly right
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic