You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Craig R. McClanahan" <Cr...@eng.sun.com> on 2000/09/15 21:11:08 UTC

Re: Context vs static variable question

Rick Bosch wrote:

> Yo,
>     I have a bean that i would like shared across jsp files in different
> directories ie
>
>     http://myserevr/app1/my.jsp
>     http://myserevr/app2/my.jsp
>     http://myserevr/app3/my.jsp
>
> I tried loading the page into a pagecontext attribute with the widest scope
> possible (APPLICATION_SCOPE) and yet each directory loads its own instance
> of the bean.  I know each directory is its own "application" but is there no
> context i can share across applications
>

There is nothing inside the servlet spec for sharing information across
contexts.  You will need to either make these three pages part of the same web
application (i.e. the same context), or use external mechanisms like databases,
EJBs, directory servers, or shared files.

>
> which brings me to my next point
>
> I could use a static variable in my bean to be sure all instances have the
> values but i know many people avoid static variables... but why.  It doesnt
> cause thread issues? Is there a perfmance hit? asocaited with static
> variables?  Can anybody explain exactly how the virtual machine handles
> static variables
>

The Java Language Specification would be your best source of info for what the
JVM actually does.  However, you'll find that there is room for innovation here
just like there is room for people to implement the servlet spec in different
ways.

My personal beef with static variables has more to do with the fact that they
are accessible from any code, any where in my web app.  This makes debugging
much more difficult than the case where you have to pass an explicit reference
to an object in order to access it.

An aspect of statics that relevant to servlets is that statics are global only
within the scope of a single class loader.  For Tomcat, that means a static you
declare in one webapp is *not* visible in other webapps, because each gets their
own class loader.  However, this behavior is not spec'd, so you cannot count on
it for portability.

>
> how would anyone else approach this situation, any disadvantages/advanteges
> across implementations.
>
> -awaiting vigorous and religious discussion
>
> -rick

Craig McClanahan

====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat