You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jonathan Reichhold <re...@singingfish.com> on 2000/07/11 04:30:09 UTC

Loading load two different contexts cause overwriting of the first one's private variables?!?

I discovered what I think is a bug in Tomcat and I want to see if others agree, or if I've made a mistaken assumption.  I've got a servlet which uses getInitParameter to load a customer name and initialize the private variable customerName.  This name is then used in the response to say hello to a given customer (1 or 2).  All seems good.  The attatched web.xml file is loaded in $TOMCAT_HOME/webapps/ROOT/WEB-INF/web.xml and I see the init lines

Init for customer1 completed
Init for customer2 completed

I then call http://localhost:8080/customer1/search and my response is "Hello customer2".  Calls to http://localhost:8080/customer2/search actually returns the expected "Hello customer2".

What is going on?  It seems to me that even thought the two seperate contexts are using the same class that they should be treated as two seperate instances in memory.  Is this assumption wrong?  What is called out in the spec?  Is this a bug?  Where would I go to start to fix this?  Where is the actual logic for loading a context and dealing with requests from know contexts handled?

Jonathan




/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*CustomerAdaptor.java*/

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

public class CustomerAdaptor extends javax.servlet.GenericServlet {
    private static String customerName = "unknown";
  
    public void init( ServletConfig config ) throws ServletException {
 super.init(config);
 customerName = config.getInitParameter( "customerName" );
 if (customerName == null) {
     customerName = "unknown";
 } else {
     System.err.println( "Init for " + customerName + " completed");
 }
    }

    public void destroy() {
 super.destroy();
    }

    public void service( javax.servlet.ServletRequest req, javax.servlet.ServletResponse res ) 
 throws ServletException, IOException
    {
 res.setContentType( "text/html" );
 ServletOutputStream out = res.getOutputStream();
 out.println( "Hello " + customerName );
    }
}

/*----------------------------------------------------------------------------------------------------------------------------*/
/*web.xml*/

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<web-app>
   <servlet>
        <servlet-name>searchCustomer1</servlet-name>
        <servlet-class>CustomerAdaptor</servlet-class>
        <init-param>
            <param-name>customerName</param-name>
            <param-value>customer1</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>searchCustomer1</servlet-name>
       <url-pattern>/customer1/search</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>searchCustomer2</servlet-name>
        <servlet-class>CustomerAdaptor</servlet-class>
        <init-param>
            <param-name>customerName</param-name>
            <param-value>customer2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>searchCustomer2</servlet-name>
       <url-pattern>/Customer2/search</url-pattern>
    </servlet-mapping>
</web-app>


Re: Loading load two different contexts cause overwriting of the first one's private variables?!?

Posted by ChenJP <ch...@email.com>.
Pls do not use static var. 

    private static String customerName = "unknown";

  ----- Original Message ----- 
  From: Jonathan Reichhold 
  To: tomcat-user@jakarta.apache.org 
  Sent: Tuesday, July 11, 2000 10:30 AM
  Subject: Loading load two different contexts cause overwriting of the first one's private variables?!?


  I discovered what I think is a bug in Tomcat and I want to see if others agree, or if I've made a mistaken assumption.  I've got a servlet which uses getInitParameter to load a customer name and initialize the private variable customerName.  This name is then used in the response to say hello to a given customer (1 or 2).  All seems good.  The attatched web.xml file is loaded in $TOMCAT_HOME/webapps/ROOT/WEB-INF/web.xml and I see the init lines
   
  Init for customer1 completed
  Init for customer2 completed

  I then call http://localhost:8080/customer1/search and my response is "Hello customer2".  Calls to http://localhost:8080/customer2/search actually returns the expected "Hello customer2".

  What is going on?  It seems to me that even thought the two seperate contexts are using the same class that they should be treated as two seperate instances in memory.  Is this assumption wrong?  What is called out in the spec?  Is this a bug?  Where would I go to start to fix this?  Where is the actual logic for loading a context and dealing with requests from know contexts handled?

  Jonathan


   
   
  /*----------------------------------------------------------------------------------------------------------------------------------------------------*/
  /*CustomerAdaptor.java*/

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

  public class CustomerAdaptor extends javax.servlet.GenericServlet {
      private static String customerName = "unknown";
    
      public void init( ServletConfig config ) throws ServletException {
   super.init(config);
   customerName = config.getInitParameter( "customerName" );
   if (customerName == null) {
       customerName = "unknown";
   } else {
       System.err.println( "Init for " + customerName + " completed");
   }
      }

      public void destroy() {
   super.destroy();
      }

      public void service( javax.servlet.ServletRequest req, javax.servlet.ServletResponse res ) 
   throws ServletException, IOException
      {
   res.setContentType( "text/html" );
   ServletOutputStream out = res.getOutputStream();
   out.println( "Hello " + customerName );
      }
  }

  /*----------------------------------------------------------------------------------------------------------------------------*/
  /*web.xml*/

  <?xml version="1.0" encoding="ISO-8859-1"?>

  <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
      "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

  <web-app>
     <servlet>
          <servlet-name>searchCustomer1</servlet-name>
          <servlet-class>CustomerAdaptor</servlet-class>
          <init-param>
              <param-name>customerName</param-name>
              <param-value>customer1</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
         <servlet-name>searchCustomer1</servlet-name>
         <url-pattern>/customer1/search</url-pattern>
      </servlet-mapping>
      <servlet>
          <servlet-name>searchCustomer2</servlet-name>
          <servlet-class>CustomerAdaptor</servlet-class>
          <init-param>
              <param-name>customerName</param-name>
              <param-value>customer2</param-value>
          </init-param>
          <load-on-startup>2</load-on-startup>
      </servlet>
      <servlet-mapping>
         <servlet-name>searchCustomer2</servlet-name>
         <url-pattern>/Customer2/search</url-pattern>
      </servlet-mapping>
  </web-app>