Brief description  about Online courses   join in Online courses

Finalizing a Servlet

Java  Teacher
Java Teacher



When a servlet container determines that a servlet should be removed from service


for example, when a container wants to reclaim memory resources, or when it is being shut down), it calls the destroy method of the Servlet interface.


In this method, you release any resources the servlet is using and save anypersistent state.


The following destroy method releases the database object createdin the init method described in Initializing a Servlet

public void destroy() {bookDB = null;}


All of a servlet’s service methods should be complete when a servlet isremoved.

The server tries to ensure this completion by calling the destroymethod only after all service requests have returned or after a server-specificgrace period, whichever comes first.If your servlet has potentially long-running service requests, use the techniquesdescribed below to do the following:•


Keep track of how many threads are currently running the servicemethod.

Provide a clean shutdown by having the destroy method notify long-runningthreads of the shutdown and wait for them to complete.

• Have the long-running methods poll periodically to check for shutdownand, if necessary, stop working, clean up, and return.


The field should have synchronizedaccess methods to increment, decrement, and return its value.


public class ShutdownExample extends HttpServlet {

private int serviceCounter = 0;


//Access methods for serviceCounter

protected synchronized void enteringServiceMethod() {




protected synchronized void leavingServiceMethod() {





protected synchronized int numServices() {

return serviceCounter;




The service method should increment the service counter each time the methodis entered and should decrement the counter each time the method returns.

This is one of the few times that your HttpServlet subclass should override the servicemethod.

The new method should call super.service to preserve all of theoriginal service method’s functionality:


protected void service(HttpServletRequest req,

                  HttpServletResponse resp)

                 throws ServletException,IOException {




    try {super.service(req, resp);



finally {






Notifying Methods to Shut Down


To ensure a clean shutdown, your destroy method should not release any sharedresources until all of the service requests have completed. One part of doing thisis to check the service counter. Another part is to notify the long-running methods that it is time to shut down.


For this notification, another field is required.


The field should have the usual access methods:


public class ShutdownExample extends HttpServlet {


private boolean shuttingDown;




//Access methods for shuttingDown


protected synchronized void setShuttingDown(boolean flag) {

shuttingDown = flag;



protected synchronized boolean isShuttingDown() {


return shuttingDown;




An example of the destroy method using these fields to provide a clean shutdownfollows:


public void destroy()



/* Check to see whether there are still service methods /*

/* running, and if there are, tell them to stop. */


if (numServices() > 0) {




/* Wait for the service methods to stop. */


while(numServices() > 0)


try {


} catch (InterruptedException e) {}




Creating Polite Long-Running Methods


The final step in providing a clean shutdown is to make any long-running methodsbehave politely.

Methods that might run for a long time should check the value of the field that notifies them of shutdowns and should interrupt their work,if necessary.


public void doPost(...) {


for(i = 0; ((i < lotsOfStuffToDo) &&!isShuttingDown());i++)


try {




catch (InterruptedException e) {





Write your comment now