With regard to automatic servlet reloading in Tomcat 3.1, what happens if one servlet makes references to other servlets in the same webapp? Will automatic reloading get enabled for all such servlets?

Avi Kak

The short answer to the question is: No.

Let's say that a webapp for which automatic reload has been enabled consists of a group of mutually referencing servlets. Let's also say that you have modified and recompiled all the servlets. When you hit the reload button on the browser, only one servlet -- the servlet that the browser is pointing to -- will be automatically reloaded by the container. Hitting the reload button on the browser for the other servlets will not cause their reload into the container and you will only see their older versions. If you want the newer versions of the other servlets to be reloaded into the container, in general you'd need to shutdown and restart the servlet container.

This behavior of Tomcat 3.1 is in keeping with the following statement that appears in Tomcat 3.1 release notes:

"...... changes to classes other than the servlet you are requesting do not trigger class reloads -- you will need to restart Tomcat to reflect changes in those classes."
In the rest of this posting, I will explain this property of Tomcat 3.1 with the help an example. The reader is also referred to a posting by Costin Manolache on this aspect of automatic class reloading in Tomcat 3.1.

To demonstrate this behavior of Tomcat 3.1, I have constructed the following example that consists of two simple mutually referencing servlets called TestServlet_1 and TestServlet_2. In my Tomcat 3.1 container, both these servlets are in a webapp called test-suite and reside in the directory TOMCAT_HOME/webapps/test-suite/WEB-INF/classes. In accordance with the jGuru FAQ posting, I have enabled automatic reloading for this webapp by including the following element in the server.xml file in the TOMCAT_HOME/conf directory:

<Context path="/test-suite" docBase="webapps/test-suite" debug="0" reloadable="true" > </Context>

Let's say that after starting the servlet container, you point your browser to the following URL:

http://localhost:8080/test-suite/servlet/TestServlet_1

the servlet container will load TestServlet_1 into the container, which will cause the browser to display a "Hello..." message. If you click inside this message where indicated, the browser will make a request for TestServlet_2 and it will be loaded into the container.

Now suppose you make changes to both the servlets by altering, say, the value of "Revision number: ". If your browser is pointing to TestServlet_1 and you hit reload button, the servlet container will automatically reload TestServlet_1. But the container will NOT reload TestServlet_2 even if you hit the reload button on the browser when the browser is pointing to TestServlet_2.

To see the newer version of TestServlet_2, you have two choices: 1) Recompile the classes and this time start out by pointing your browser to TestServlet_2. Or, 2) Shutdown and restart the servlet container.

Here is the code for the two servlets:


//////////   file: TestServlet_1.java   //////////

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


public class TestServlet_1 extends HttpServlet {

  public void init() throws ServletException 
  {
    System.out.println("TestServlet_1 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_1 </title></head>" +
                 "Hello there from TestServlet_1 -------  " +
                 "To see the hello message from TestServlet_2," +
                 " click <a href="/test-suite/servlet/TestServlet_2">here</a>  ------  " +
                 "Revision number: 17" +
                 "</body></html>" );
  }
}



//////////    file:  TestServlet_2.java   //////////

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


public class TestServlet_2 extends HttpServlet {

  public void init() throws ServletException 
  {
    System.out.println("TestServlet_2 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_2 </title></head>" +
                 "TestServlet_2 saying hello  -------  " +
                 "To see the hello message from TestServlet_1," +
                 " click <a href="/test-suite/servlet/TestServlet_1">here</a>  ------  " +
                 "Revision number: 17" +
                 "</body></html>" );
  }
}

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


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

 

 

About | Sitemap | Contact