Jeffrey & Jacqueline Cooke wrote:

> I'm trying to move my servlets from Jrun (LiveSoftware) to Jserv, however
> I've noticed a slight difference in behaviour that I'm not entirely sure how
> to get around?
>
> Here's a simplified example...
>
> I have two servlets, ServletA and ServletB.  Both of these servlets access a
> common class called ClassC.  ServletA, ServletB and ClassC were all compiled
> into the same package. ClassC defines a static variable VariableD, and
> provides a method to set the variable and to get its value.  The process is
> that ServletA is started by the servlet engine and reads a configuration
> file.  ServletA sets the static variable VariableD in ClassC.  Later on
> ServletB is started and it accesses ClassC and reads the value of the static
> variable.  ServletB is able to get the value from ClassC that was set by
> ServletA.
>
> In Jrun this works well and provides a means of 'sharing' values between
> servlets.  Or looking at it in another way, of making a set of global
> variables for the package.
>
> When I run the same servlets in JServ...  ServletB sees a different value in
> ClassC.VariableD than ServletA does? (almost as if they're looking at two
> different instances of the static variable?)
>
> Assuming that this method of sharing values between servlets is not good
> programming and has simply relied on a quirk in Jrun's environment, how
> would I implement value sharing between servlets in Jserv?
>
> Thanks
> Jeff Cooke
> Midland TX
>

It sounds like you are experiencing the effects of the way Java class loaders
work.  If a class happens to be loaded by two different class loaders, they are
in fact different classes -- static variables are not shared, and attempts to
cast a reference from one class loader to an instance of the other will fail.

Apache JServ, like most servlet engines, implements a custom class loader so
that you can automatically reload servlets when the code is changed.  Classes
loaded from the system CLASSPATH cannot do this.  You configure the special
class path for the servlets with the "repositories" line in your zone
properties file.

What you want to do is make sure that ServletA, ServletB, and ClassC are all
loaded by the same class loader.  The easiest way to do that is put them in
your system CLASSPATH -- but you give up the auto-reload functionality in this
case.  Alternatively, make sure that ServletA, ServletB, and ClassC are all
visible via a "repositories" listing in the zone properties file for the same
zone, and ***not*** visible through the system class path.  Then, static
variables will really be shared.

Craig McClanahan




--
--------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
READ THE FAQ!!!!     <http://java.apache.org/faq/>
Archives and Other:  <http://java.apache.org/main/mail.html/>
Problems?:           [EMAIL PROTECTED]

Reply via email to