i have a question regarding the destroy method of a servlet, for example a have a simple application which uses jsp as the view and servlet as a controller and this is a database driven...if closing my database connection in the destroy method of a servlet, so if the first jsp submitted the request to the servlet and the servlet makes the connection and query some data and after that forward that request (data) to the second jsp, does the destroy method ever called? thanks... raymond
Raymond If I understand your question correctly, you're asking whether the destroy() method is called when you forward your request to another resource? The destroy() method is only called once on a servlet instance, and when the container decides it no longer needs the instance to service requests, and only if the instance has no more request threads to service. As for destroy(), the init() method is only called once in the life-cycle of a servlet instance. So... The init() method is a good place to do your database connection work, and the destoy() method is a good place to shut it down again.
Joined: Jan 29, 2001
so u mean the container is the one who decides to call the destroy method? what if i closed the browser does the servlet destroy method called? thanks...
The only time destroy() is called is immediately before the object (the instance of the servlet) is removed from memory. It's like any other classes finalize() method.
The container decides when to remove servlets from service. Just because the servlet has finished processing your request, doesn't mean it won't be kept around to service another request by someone else. In fact most (all?) containers do exactly this. Even with SingleThreadModel servlets.