You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Vladimir Grishchenko <vl...@hotmail.com> on 2001/08/22 17:49:12 UTC

multiple context paths pointing to the same doc base?

Hi there,

May be somebody can give me a quick answer with regards to T3.2.2.

I need to have multiple context paths to point to the same doc base,
say my server.xml has the following entries:

 <Context path="/somectx"
          docBase="d:/foo/bar"
          debug="0"
          reloadable="false" >
 </Context>


 <Context path="/somectx1"
          docBase="d:/foo/bar"
          debug="0"
          reloadable="false" >
 </Context>

Will the above config cause my webapp classes/servlets to be loaded
twice through different classloaders, or TC is smart enough to realize
it's the same webapp?

Thanks,
--V.

Re: Connection pooling Question

Posted by Bo Xu <bo...@cybershop.ca>.
"A.L." wrote:

> I have created a servlet class (using
> DBConenctionbroker from javaexchange) which upon
> initialization creates a number of database
> conenctions.   The init method is the only method
> defined.
>
> My plan now is to create a number of servlets which
> extend this class by defining doGet methods, which
> will also return specific information from a database.
>  My question is if each of these classes are child
> classes of my connection broker class, how connection
> brokers will be created.  In other words:
> Lets say I hae my connection broker and three child
> classes. If upon initialization my connectionBroker
> class actually creates 3 database connections.  How
> many database conenctions will there be once all the
> servlets are initialized?
>
> Will there simply be 3 for the connectionBroker,
> 9 for each of the child servlets,
> or twelve for all the servlets?
>
> Again I will probably have all 4 servlets intialize
> upon tomcat startup.
>
> Thanks,
>    -Amos
> [...]

I am not sure, I think it is possible to use "static class field" to
control/count the number
of instances if there is only-one classloader, just as a reference :-)


Bo
Aug.22, 2001



RE: Connection pooling Question

Posted by Filip Hanik <ma...@filip.net>.
and where does Tomcat fit into this?
sounds like a design issue, try making a search in google.com for java
connection pools. that should take care of you

Filip

~
Namaste - I bow to the divine in you
~
Filip Hanik
Software Architect
filip@filip.net
www.filip.net

>-----Original Message-----
>From: A.L. [mailto:amoslieber@yahoo.com]
>Sent: Wednesday, August 22, 2001 2:57 PM
>To: tomcat-user@jakarta.apache.org
>Subject: Connection pooling Question
>
>
>I have created a servlet class (using
>DBConenctionbroker from javaexchange) which upon
>initialization creates a number of database
>conenctions.   The init method is the only method
>defined.
>
>My plan now is to create a number of servlets which
>extend this class by defining doGet methods, which
>will also return specific information from a database.
> My question is if each of these classes are child
>classes of my connection broker class, how connection
>brokers will be created.  In other words:
>Lets say I hae my connection broker and three child
>classes. If upon initialization my connectionBroker
>class actually creates 3 database connections.  How
>many database conenctions will there be once all the
>servlets are initialized?
>
>Will there simply be 3 for the connectionBroker,
>9 for each of the child servlets,
>or twelve for all the servlets?
>
>Again I will probably have all 4 servlets intialize
>upon tomcat startup.
>
>Thanks,
>   -Amos
>
>__________________________________________________
>Do You Yahoo!?
>Make international calls for as low as $.04/minute with Yahoo! Messenger
>http://phonecard.yahoo.com/
>


Connection pooling Question

Posted by "A.L." <am...@yahoo.com>.
I have created a servlet class (using
DBConenctionbroker from javaexchange) which upon
initialization creates a number of database
conenctions.   The init method is the only method
defined.

My plan now is to create a number of servlets which
extend this class by defining doGet methods, which
will also return specific information from a database.
 My question is if each of these classes are child
classes of my connection broker class, how connection
brokers will be created.  In other words:
Lets say I hae my connection broker and three child
classes. If upon initialization my connectionBroker
class actually creates 3 database connections.  How
many database conenctions will there be once all the
servlets are initialized?

Will there simply be 3 for the connectionBroker,
9 for each of the child servlets,     
or twelve for all the servlets?

Again I will probably have all 4 servlets intialize
upon tomcat startup.

Thanks,
   -Amos

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Re: multiple context paths pointing to the same doc base?

Posted by Dmitri Colebatch <di...@bigpond.net.au>.
On Wed, 22 Aug 2001, Craig R. McClanahan wrote:
> Since the original objective was to share the cache, it seems to me that
> one versus two contexts is not a big deal, right?

ahhh, sorry - missed the start of the thread... thought it was a more
general "one webapp" thing...

> > you would be well advised to put it in a jar which you then place in
> > tomcat's lib dir... that way its relatively easy to move your app.
> >
> 
> In fact, this is critical to the sharing solution.  Eerything in Tomcat's
> lib directory is shared -- everything in /WEB-INF/classes or /WEB-INF/lib
> is not shared.

My reading of the suggestion was that the classes would reside elsewhere
in the system but be in the startup classpath as startup - this would do
the same thing but be a little more spread out... thats all my point was
there (o:

cheesr
dim


Re: multiple context paths pointing to the same doc base?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 23 Aug 2001, Dmitri Colebatch wrote:

> Date: Thu, 23 Aug 2001 06:42:13 +1000
> From: Dmitri Colebatch <di...@bigpond.net.au>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Re: multiple context paths pointing to the same doc base?
>
> On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:
>
> > >
> > > One option you have is to put the classes that actually cache things into
> > > a common directory ($TOMCAT_HOME/lib or $CATALINA_HOME/lib).  Such classes
> > > are shared across web apps, so this should accomplish your goal of having
> > > a single cache.  They are loaded from a (common) parent class loader to
> > > the one for each web app.
>
> Wont this still result in one init() call per servlet per context?

Yes, but the second initialization can easily detect that the cache has
already been initialized.

>  The
> only think you'll gain is the use of static variables right?  Obviously
> these could be used to achieve the desired outcome, but you wouldn't get
> "one context with two paths"...?
>

Since the original objective was to share the cache, it seems to me that
one versus two contexts is not a big deal, right?

> > >
> >
> > I'm not using the latest servlet spec (2.3). Anyway, the simplest solutuion
> > will be to put all my classes
> > on the system classpath, this should  work. I have no jsp's and don't need
> > dynamic class
> > reloading in production, so I beleive it's perfectly legal in my case.
>
> you would be well advised to put it in a jar which you then place in
> tomcat's lib dir... that way its relatively easy to move your app.
>

In fact, this is critical to the sharing solution.  Eerything in Tomcat's
lib directory is shared -- everything in /WEB-INF/classes or /WEB-INF/lib
is not shared.

> cheers
> dim
>
>

Craig



Re: multiple context paths pointing to the same doc base?

Posted by Dmitri Colebatch <di...@bigpond.net.au>.
On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:

> >
> > One option you have is to put the classes that actually cache things into
> > a common directory ($TOMCAT_HOME/lib or $CATALINA_HOME/lib).  Such classes
> > are shared across web apps, so this should accomplish your goal of having
> > a single cache.  They are loaded from a (common) parent class loader to
> > the one for each web app.

Wont this still result in one init() call per servlet per context?  The
only think you'll gain is the use of static variables right?  Obviously
these could be used to achieve the desired outcome, but you wouldn't get
"one context with two paths"...?

> >
> 
> I'm not using the latest servlet spec (2.3). Anyway, the simplest solutuion
> will be to put all my classes
> on the system classpath, this should  work. I have no jsp's and don't need
> dynamic class
> reloading in production, so I beleive it's perfectly legal in my case.

you would be well advised to put it in a jar which you then place in
tomcat's lib dir... that way its relatively easy to move your app.

cheers
dim


Re: multiple context paths pointing to the same doc base?

Posted by Vladimir Grishchenko <vl...@hotmail.com>.
>
> One option you have is to put the classes that actually cache things into
> a common directory ($TOMCAT_HOME/lib or $CATALINA_HOME/lib).  Such classes
> are shared across web apps, so this should accomplish your goal of having
> a single cache.  They are loaded from a (common) parent class loader to
> the one for each web app.
>

I'm not using the latest servlet spec (2.3). Anyway, the simplest solutuion
will be to put all my classes
on the system classpath, this should  work. I have no jsp's and don't need
dynamic class
reloading in production, so I beleive it's perfectly legal in my case.

Thanks and regards,

--V.


Re: multiple context paths pointing to the same doc base?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:

> Date: Wed, 22 Aug 2001 11:19:47 -0700
> From: Vladimir Grishchenko <vl...@hotmail.com>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Re: multiple context paths pointing to the same doc base?
>
> > > Thanks for the clarification, it makes sense.
> > > Is it according to the spec or just a peculiarity of Tomcat?
> > >
> >
> > Servlet 2.3 PFD2, Section 3.1 (emphasis added by **):
> >
> >   A ServletContext is rooted at *a* known
> >   path within the web server.  For example,
> >   a servlet context could be located at
> >   http://www.mycorp.com/catalog.  All requests
> >   that begin with the /catalog request path,
> >   known as the context path, are routed to
> >   the web application associated with the
> >   servlet context.
> >
>
> Well, I don't want to be picky but this quote doesn't emphasize
> that context is the same thing as web application, nor does it
> say that a web application cannot be shared by multiple contexts , it merely
> says that all requests are routed to the app associated with
> a particular context.

Spec language is like legal contracts ... you've got to be familiar with
the nuances :-).  In particular the "a" above means "one and only one".

> In my understanding (which might be wrong btw)
> a webapp is something you package and distribute in a *.war file meaning
> classes, servlets, jsps, static html, and a webapp can be shared by
> different
> contexts.

>From the perspective of the container, it is free (from a spec
perspective) to do whatever it wants to with a WAR file.  Some containers
automatically expand them into unpacked directories (like Tomcat does by
default), some run directly from a WAR (like Tomcat can), some store the
static resources as BLOBs in a database.  But the key point is this --
where the resources came from is irrelevant -- each web application is
operated independently.

> Isn't it more correct to have a class loader per *.war file
> (logically) than per contextpath? Or have it configurable since  the spec
> is somewhat vague on this point?

You'll have to find me a servlet container vendor that interprets this the
way you would like in order for me to think it is vague.

>
> > > Actually, I need just the opposite, is there any way to do this? Meaning
> > > different context paths - one webapp and one class loader...
> > >
> >
> > Not that I know of.
> >
> > Of course, that begs the question of *why* you need this -- if everything
> > is identical (as it would be if you got your wish), then what's the point?
>
> Without going into much details my particular application is relying on
> combination
> of context path and path info values to perform an internal lookup of some
> data.
> The data is cached and there's a cache maintanence thread running. So, I end
> up
> with as many caches and threads as many contexts I have configured. Of
> course
> I can try to interpret request URI's to extract the data I need, but using
> context path
> is much easier. Well, I guess I have no choice now :-|
>

One option you have is to put the classes that actually cache things into
a common directory ($TOMCAT_HOME/lib or $CATALINA_HOME/lib).  Such classes
are shared across web apps, so this should accomplish your goal of having
a single cache.  They are loaded from a (common) parent class loader to
the one for each web app.

The only tricky part of this would be where the classes of the objects
being cached are found.  If your cache logic is in the parent class
loader, but you try to instantiate an object of a class in the web app
using the usual technique (Class.forName().newInstance()), you will get
ClassNotFoundException errors.  This can be worked around by noting that
the Thread context class loader (guaranteed at least for Servlet 2.3 based
containers) will always correspond to the webapp class loader, so your
cache code can do something like this:

  String applicationClassName = ... class name of the object to create ...
  ClassLoader ccl = Thread.currentThread().getContextClassLoader();
  Class clazz = ccl.loadClass(applicationClassName);
  Object instance = clazz.newInstance();

or something similar.

> --V.
>

Craig


Re: multiple context paths pointing to the same doc base?

Posted by Vladimir Grishchenko <vl...@hotmail.com>.
> > Thanks for the clarification, it makes sense.
> > Is it according to the spec or just a peculiarity of Tomcat?
> >
>
> Servlet 2.3 PFD2, Section 3.1 (emphasis added by **):
>
>   A ServletContext is rooted at *a* known
>   path within the web server.  For example,
>   a servlet context could be located at
>   http://www.mycorp.com/catalog.  All requests
>   that begin with the /catalog request path,
>   known as the context path, are routed to
>   the web application associated with the
>   servlet context.
>

Well, I don't want to be picky but this quote doesn't emphasize
that context is the same thing as web application, nor does it
say that a web application cannot be shared by multiple contexts , it merely
says that all requests are routed to the app associated with
a particular context. In my understanding (which might be wrong btw)
a webapp is something you package and distribute in a *.war file meaning
classes, servlets, jsps, static html, and a webapp can be shared by
different
contexts. Isn't it more correct to have a class loader per *.war file
(logically) than per contextpath? Or have it configurable since  the spec
is somewhat vague on this point?

> > Actually, I need just the opposite, is there any way to do this? Meaning
> > different context paths - one webapp and one class loader...
> >
>
> Not that I know of.
>
> Of course, that begs the question of *why* you need this -- if everything
> is identical (as it would be if you got your wish), then what's the point?

Without going into much details my particular application is relying on
combination
of context path and path info values to perform an internal lookup of some
data.
The data is cached and there's a cache maintanence thread running. So, I end
up
with as many caches and threads as many contexts I have configured. Of
course
I can try to interpret request URI's to extract the data I need, but using
context path
is much easier. Well, I guess I have no choice now :-|

--V.

>
> > Thanks,
> > --V.
> >
>
> Craig
>
>
> > ----- Original Message -----
> > From: "Craig R. McClanahan" <cr...@apache.org>
> > To: <to...@jakarta.apache.org>
> > Sent: Wednesday, August 22, 2001 9:41 AM
> > Subject: Re: multiple context paths pointing to the same doc base?
> >
> >
> > >
> > >
> > > On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:
> > >
> > > > Hi there,
> > > >
> > > > May be somebody can give me a quick answer with regards to T3.2.2.
> > > >
> > > > I need to have multiple context paths to point to the same doc base,
> > > > say my server.xml has the following entries:
> > > >
> > > >  <Context path="/somectx"
> > > >           docBase="d:/foo/bar"
> > > >           debug="0"
> > > >           reloadable="false" >
> > > >  </Context>
> > > >
> > > >
> > > >  <Context path="/somectx1"
> > > >           docBase="d:/foo/bar"
> > > >           debug="0"
> > > >           reloadable="false" >
> > > >  </Context>
> > > >
> > > > Will the above config cause my webapp classes/servlets to be loaded
> > > > twice through different classloaders,
> > >
> > > Yes.
> > >
> > > > or TC is smart enough to realize
> > > > it's the same webapp?
> > > >
> > >
> > > It is *not* the same webapp.
> > >
> > > Every webapp must have a unique context path, and you have just
defined
> > > two different context paths here.  The fact that the document base is
the
> > > same is just a coincidence.
> > >
> > > > Thanks,
> > > > --V.
> > > >
> > >
> > > Craig
> > >
> > >
> > >
> >
>
>

Re: multiple context paths pointing to the same doc base?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:

> Thanks for the clarification, it makes sense.
> Is it according to the spec or just a peculiarity of Tomcat?
>

Servlet 2.3 PFD2, Section 3.1 (emphasis added by **):

  A ServletContext is rooted at *a* known
  path within the web server.  For example,
  a servlet context could be located at
  http://www.mycorp.com/catalog.  All requests
  that begin with the /catalog request path,
  known as the context path, are routed to
  the web application associated with the
  servlet context.

> Actually, I need just the opposite, is there any way to do this? Meaning
> different context paths - one webapp and one class loader...
>

Not that I know of.

Of course, that begs the question of *why* you need this -- if everything
is identical (as it would be if you got your wish), then what's the point?

> Thanks,
> --V.
>

Craig


> ----- Original Message -----
> From: "Craig R. McClanahan" <cr...@apache.org>
> To: <to...@jakarta.apache.org>
> Sent: Wednesday, August 22, 2001 9:41 AM
> Subject: Re: multiple context paths pointing to the same doc base?
>
>
> >
> >
> > On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:
> >
> > > Hi there,
> > >
> > > May be somebody can give me a quick answer with regards to T3.2.2.
> > >
> > > I need to have multiple context paths to point to the same doc base,
> > > say my server.xml has the following entries:
> > >
> > >  <Context path="/somectx"
> > >           docBase="d:/foo/bar"
> > >           debug="0"
> > >           reloadable="false" >
> > >  </Context>
> > >
> > >
> > >  <Context path="/somectx1"
> > >           docBase="d:/foo/bar"
> > >           debug="0"
> > >           reloadable="false" >
> > >  </Context>
> > >
> > > Will the above config cause my webapp classes/servlets to be loaded
> > > twice through different classloaders,
> >
> > Yes.
> >
> > > or TC is smart enough to realize
> > > it's the same webapp?
> > >
> >
> > It is *not* the same webapp.
> >
> > Every webapp must have a unique context path, and you have just defined
> > two different context paths here.  The fact that the document base is the
> > same is just a coincidence.
> >
> > > Thanks,
> > > --V.
> > >
> >
> > Craig
> >
> >
> >
>


Re: multiple context paths pointing to the same doc base?

Posted by Vladimir Grishchenko <vl...@hotmail.com>.
Thanks for the clarification, it makes sense.
Is it according to the spec or just a peculiarity of Tomcat?

Actually, I need just the opposite, is there any way to do this? Meaning
different context paths - one webapp and one class loader...

Thanks,
--V.

----- Original Message ----- 
From: "Craig R. McClanahan" <cr...@apache.org>
To: <to...@jakarta.apache.org>
Sent: Wednesday, August 22, 2001 9:41 AM
Subject: Re: multiple context paths pointing to the same doc base?


> 
> 
> On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:
> 
> > Hi there,
> >
> > May be somebody can give me a quick answer with regards to T3.2.2.
> >
> > I need to have multiple context paths to point to the same doc base,
> > say my server.xml has the following entries:
> >
> >  <Context path="/somectx"
> >           docBase="d:/foo/bar"
> >           debug="0"
> >           reloadable="false" >
> >  </Context>
> >
> >
> >  <Context path="/somectx1"
> >           docBase="d:/foo/bar"
> >           debug="0"
> >           reloadable="false" >
> >  </Context>
> >
> > Will the above config cause my webapp classes/servlets to be loaded
> > twice through different classloaders,
> 
> Yes.
> 
> > or TC is smart enough to realize
> > it's the same webapp?
> >
> 
> It is *not* the same webapp.
> 
> Every webapp must have a unique context path, and you have just defined
> two different context paths here.  The fact that the document base is the
> same is just a coincidence.
> 
> > Thanks,
> > --V.
> >
> 
> Craig
> 
> 
> 

RE: static variables in multiple contexts PLEASE HELP

Posted by Zach Hollandsworth <zh...@quarem.com>.
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: tomcat-user@jakarta.apache.org; zholland@quarem.com
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 <zh...@quarem.com>
> Reply-To: tomcat-user@jakarta.apache.org, zholland@quarem.com
> To: tomcat-user@jakarta.apache.org
> 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: tomcat-user@jakarta.apache.org; zholland@quarem.com
> 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 <zh...@quarem.com>
> > Reply-To: tomcat-user@jakarta.apache.org, zholland@quarem.com
> > To: tomcat-user@jakarta.apache.org
> > 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

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Thu, 23 Aug 2001, Zach Hollandsworth wrote:

> Date: Thu, 23 Aug 2001 18:37:11 -0500
> From: Zach Hollandsworth <zh...@quarem.com>
> Reply-To: tomcat-user@jakarta.apache.org, zholland@quarem.com
> To: tomcat-user@jakarta.apache.org
> 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: tomcat-user@jakarta.apache.org; zholland@quarem.com
> 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 <zh...@quarem.com>
> > Reply-To: tomcat-user@jakarta.apache.org, zholland@quarem.com
> > To: tomcat-user@jakarta.apache.org
> > 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

Posted by Zach Hollandsworth <zh...@quarem.com>.
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: tomcat-user@jakarta.apache.org; zholland@quarem.com
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 <zh...@quarem.com>
> Reply-To: tomcat-user@jakarta.apache.org, zholland@quarem.com
> To: tomcat-user@jakarta.apache.org
> 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

Posted by "Craig R. McClanahan" <cr...@apache.org>.
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 <zh...@quarem.com>
> Reply-To: tomcat-user@jakarta.apache.org, zholland@quarem.com
> To: tomcat-user@jakarta.apache.org
> 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
>
>
>


static variables in multiple contexts PLEASE HELP

Posted by Zach Hollandsworth <zh...@quarem.com>.
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: multiple context paths pointing to the same doc base?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Wed, 22 Aug 2001, Vladimir Grishchenko wrote:

> Hi there,
>
> May be somebody can give me a quick answer with regards to T3.2.2.
>
> I need to have multiple context paths to point to the same doc base,
> say my server.xml has the following entries:
>
>  <Context path="/somectx"
>           docBase="d:/foo/bar"
>           debug="0"
>           reloadable="false" >
>  </Context>
>
>
>  <Context path="/somectx1"
>           docBase="d:/foo/bar"
>           debug="0"
>           reloadable="false" >
>  </Context>
>
> Will the above config cause my webapp classes/servlets to be loaded
> twice through different classloaders,

Yes.

> or TC is smart enough to realize
> it's the same webapp?
>

It is *not* the same webapp.

Every webapp must have a unique context path, and you have just defined
two different context paths here.  The fact that the document base is the
same is just a coincidence.

> Thanks,
> --V.
>

Craig