static variables in multiple contexts PLEASE HELP

2001-08-23 Thread Zach Hollandsworth

I have asked this question a couple of times in here with no replies.  I
have two separate contexts with the same class.  The class has a static
variable and I would like to have different static variables across
contexts.  the source to an example class is as follows:

public class StaticTest extends HttpServlet
{
private static int test = 0;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
response.getWriter().println(getTest());
}

public static int getTest()
{
if (test == 0)
test = new Random().nextInt();
return test;
}
}


It appears to work as I expect with a different number for each context it
is placed in on a windows platform, but on linux, the numbers returned are
the same and the static is set once!


should it be?


thanks
Zach





static variables between contexts

2001-08-23 Thread Zach Hollandsworth

It is not running under its own JVM, but each webapp *does* run under its
own classloader. Static variables are global only within the scope of a
particular classloader, so each webapp that uses your class gets its own if
the class was loaded from WEB-INF/classes or WEB-INF/lib. 
-Craig McClanahan, 09/15/2000

If the above is true, then why do I get the SAME VALUE for getTest() when
run under different contexts on?  The static test is already initialized on
the second context that I invoke it under?  and this issues only occurs
under linux, it works fine under win32.

Give the code below:
public class StaticTest extends HttpServlet
{
private static int test = 0;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
response.getWriter().println(getTest());
}

public static int getTest()
{
if (test == 0)
test = new Random().nextInt();
return test;
}
}



Thanks Zach





RE: static variables in multiple contexts PLEASE HELP

2001-08-23 Thread Zach Hollandsworth

Any idea why my static variables are carrying across contexts then?  another
issue that I noticed and I think this is the same, but my servlet
StaticTest is usable from the examples context and it isn't even in that
context.  Its as if the contexts do not mean anything, as if they are
arbitrary?!  any ideas?

Zach

-Original Message-
From: craigmcc@localhost [mailto:craigmcc@localhost]On Behalf Of Craig
R. McClanahan
Sent: Thursday, August 23, 2001 6:13 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Re: static variables in multiple contexts PLEASE HELP


This was answered (again, it's a popular question) yesterday.

Whether a static is global or not depends on where the class is loaded
from.  If it's loaded from /WEB-INF/classes or /WEB-INF/lib, the static is
local to your web app.  If the class is loaded from Tomcat's lib
directory, or from the CLASSPATH, it is global to all web apps.

Craig


On Thu, 23 Aug 2001, Zach Hollandsworth wrote:

 Date: Thu, 23 Aug 2001 16:27:14 -0500
 From: Zach Hollandsworth [EMAIL PROTECTED]
 Reply-To: [EMAIL PROTECTED], [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Subject: static variables in multiple contexts  PLEASE HELP

 I have asked this question a couple of times in here with no replies.  I
 have two separate contexts with the same class.  The class has a static
 variable and I would like to have different static variables across
 contexts.  the source to an example class is as follows:

 public class StaticTest extends HttpServlet
 {
   private static int test = 0;
   public void doGet(HttpServletRequest request, HttpServletResponse
response)
 throws IOException
   {
   response.getWriter().println(getTest());
   }

   public static int getTest()
   {
   if (test == 0)
   test = new Random().nextInt();
   return test;
   }
 }


 It appears to work as I expect with a different number for each context it
 is placed in on a windows platform, but on linux, the numbers returned are
 the same and the static is set once!


 should it be?


 thanks
 Zach









RE: static variables in multiple contexts PLEASE HELP

2001-08-23 Thread Zach Hollandsworth

Craig,

Thanks for all of your help.  Based on what you were saying I went and
really started digging and found out that my hosting company had hard
coded the classes directory into the class path!

Thanks again
Zach

-Original Message-
From: craigmcc@localhost [mailto:craigmcc@localhost]On Behalf Of Craig
R. McClanahan
Sent: Thursday, August 23, 2001 8:18 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: RE: static variables in multiple contexts PLEASE HELP


On Thu, 23 Aug 2001, Zach Hollandsworth wrote:

 Date: Thu, 23 Aug 2001 18:37:11 -0500
 From: Zach Hollandsworth [EMAIL PROTECTED]
 Reply-To: [EMAIL PROTECTED], [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Subject: RE: static variables in multiple contexts  PLEASE HELP

 Any idea why my static variables are carrying across contexts then?
another
 issue that I noticed and I think this is the same, but my servlet
 StaticTest is usable from the examples context and it isn't even in that
 context.  Its as if the contexts do not mean anything, as if they are
 arbitrary?!  any ideas?


Did you *read* my answer?

Where are the class files that contain your static variables?  Or the
class StaticTest?

If these classes are in $TOMCAT_HOME, or on your CLASSPATH, then crossing
context boundaries is correct behavior.  All of these classes are global
to *all* web apps.  (The fact that you can execute StaticTest from the
examples web app *strongly* suggests that this is what is going on).

If these classes are inside /WEB-INF/classes (or in a JAR file inside
/WEB-INF/lib) inside your web app, then the statics will *not* be global
-- they will be local to that web app.

 Zach


Craig


 -Original Message-
 From: craigmcc@localhost [mailto:craigmcc@localhost]On Behalf Of Craig
 R. McClanahan
 Sent: Thursday, August 23, 2001 6:13 PM
 To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
 Subject: Re: static variables in multiple contexts PLEASE HELP


 This was answered (again, it's a popular question) yesterday.

 Whether a static is global or not depends on where the class is loaded
 from.  If it's loaded from /WEB-INF/classes or /WEB-INF/lib, the static is
 local to your web app.  If the class is loaded from Tomcat's lib
 directory, or from the CLASSPATH, it is global to all web apps.

 Craig


 On Thu, 23 Aug 2001, Zach Hollandsworth wrote:

  Date: Thu, 23 Aug 2001 16:27:14 -0500
  From: Zach Hollandsworth [EMAIL PROTECTED]
  Reply-To: [EMAIL PROTECTED], [EMAIL PROTECTED]
  To: [EMAIL PROTECTED]
  Subject: static variables in multiple contexts  PLEASE HELP
 
  I have asked this question a couple of times in here with no replies.  I
  have two separate contexts with the same class.  The class has a static
  variable and I would like to have different static variables across
  contexts.  the source to an example class is as follows:
 
  public class StaticTest extends HttpServlet
  {
  private static int test = 0;
  public void doGet(HttpServletRequest request, HttpServletResponse
 response)
  throws IOException
  {
  response.getWriter().println(getTest());
  }
 
  public static int getTest()
  {
  if (test == 0)
  test = new Random().nextInt();
  return test;
  }
  }
 
 
  It appears to work as I expect with a different number for each context
it
  is placed in on a windows platform, but on linux, the numbers returned
are
  the same and the static is set once!
 
 
  should it be?
 
 
  thanks
  Zach
 
 
 









Contexts, Classes, Variables

2001-05-18 Thread Zach Hollandsworth

1) In Tomcat 3.2.1 do separate contexts see static variables of a class the
same?  Or do they have separate ones?

2) different versions of the same class in two contexts on the same tomcat?
(placed in the /WEB-INF/classes directory of that context)

Zach Hollandsworth