When I recompile a servlet which is already loaded by the container, it is automatically reloaded. However the destroy() method of the servlet does not get called. Does this mean that the servlet is not unloaded before reloading?

Avi Kak

The observation you make does not apply to the version of Tomcat 3.1 that is currently available for download from the Jakarta-Tomcat site. However, it does apply to Tomcat 3.2 beta 4. More on 3.2 later in this posting. First, I'd like to say a few words about what I am basing my 3.1 answer on.

The test servlet for which I show code below has both an init() method and a destroy() method. When the init() method is invoked, it prints out the following message in the Tomcat window:


       TestServlet being loaded into the container

and when the destroy() method is invoked, it prints out the following message in the Tomcat window:

       TestServlet about to be unloaded

Note that the init() method will only be invoked when the servlet class is loaded into the container and its instance created. The destroy() method will only be invoked just prior to the container unloading the servlet.

If I carry out the following steps in the sequence indicated:

  1. request the servlet through a client browser
  2. recompile the servlet
  3. hit the reload button on the browser
I get the following sequence of messages in the Tomcat window:

       TestServlet being loaded into the container

       TestServlet about to be unloaded

       RELOAD!!!
 
       TestServlet being loaded into the container

This shows the servlet is actually unloaded and its destroy() method invoked before the new version of the servlet is automatically loaded in.

Here is the code for the servlet:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class TestServlet extends HttpServlet {

  public void init() throws ServletException 
  {
    System.out.println("TestServlet being loaded into the container");
  }


  public void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException
  {
    response.setContentType( "text/html" );
    PrintWriter out = response.getWriter();

    out.println( "<html>" +  
                 "<head><title> TestServlet </title></head>" +
                 "Hello there from TestServlet" +
                 "</body></html>" );
    out.close();
  }


  public void destroy()
  {
    System.out.println( "TestServlet about to be unloaded" );
  }
}

Therefore, with regard to automatic loading and unloading, Tomcat 3.1 works as specified in the Servlet 2.1 API. However, the beta 4 version of Tomcat 3.2 seems to have a bug in it. When you do exactly the same thing as described above in Tomcat 3.2, the destroy() method is not invoked, meaning that the old copy of the servlet does not get unloaded.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact