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?
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook