The destroy() and init() methods are called only ONCE in a servlet's lifetime while the service() method may be called multiple times.
The init() method is called after the container loads the servlet class and instantiates and then wants to initialize. It is never called again.
Similarly, the destroy() method is called only once, when the servlet instance is going to be destroyed and never again ( the servlet is history, how could it be called again? ) Now, when the servlet is going to be unloaded depends on the container. It will definitely be unloaded if it is still present when the container shuts down or the application shuts down.
But, it may also be unloaded if the container decides that there is a shortage of memory and that this particular servlet hasn't got a request in a long time and so, is a prime candidate to be removed.
In either case, destroy() will be called ( only this one time ) by the container to tell the servlet that it's going to be unloaded and so it should perform whatever clean up it needs to and prepare to be destroyed.
EDIT: Ok, this is really strange and frustrating and makes me look bad ( ). When I start replying to a post, like this one, I see no replies. But when I finish and reply, earlier replies suddenly appear and judging by the timestamps they don't seem like they were posted in the time I replied, because I don't type that slow! [ September 29, 2007: Message edited by: Tarun Yadav ]
Hi Amit , do you mean to say that sessions sruvival across shutdowns and restarts is mandatory by the specification or it is vendor specific? In case of tomcat, does a call to a session getter method returns a new sessoon or fetch that "maintained by the container" if the applicaition crashes after acquiring a session and then restarts?
You get the old session only when you have configured some sort of persistance storage (e.g. file system, database etc.) for sessions (which is generally configured in the container's conf file) and have made always save session element to true. In case of resin server it is specified in resin.conf.
If you do above configuration then you will get the old session on the container restart/crash.
Well, these configuration are handled in the vendor specific ways.
-Amit Goyal [ October 01, 2007: Message edited by: Amit Goyal ]