"Praveen Kumar S ." wrote:

> Craig,
>
> > I did not understand what you ment by "shared across all the threads..."
>

I was not as clear as I should have been.  Basically, when you declare a
variable like this:

<%!
    private int myVariable = 0;
%>

you are declaring an instance variable in the generated servlet class for this
page, exactly as if you had done this in Java:

public class MyClass {

    private int myVariable = 0;

    ...

}

Now, as long as you have only a single request active at a time, this causes
you no grief.  However, if your application receives two (or more) requests at
the same time for the same page, the threads responding to each request will
all be active in the same instance of your JSP page's generated class, all
accessing the same "myVariable" value.

This is fine for read-only things that you want to share.  However, if you use
this for instance variables that can be modified, and you try to use them to
store things temporarily for a single request, you will find that instance
variables will be scribbled on.


> >
> > Did you mean they are variables with scope as "application" (if we
> > consider the JSP Scope semantics)
> >
> > If that is what you mean, then it is not so as per the specs'.
> > If you have encountered it... well I don't know if this is an inconsistent
> > behavior...
>

Beans with scope application are in fact shared across all JSP pages (and
servlets) in the application.  If you refer to such a bean in one (or more) JSP
pages, and then call some of its methods, those methods had better be thread
safe, for essentially the same reason that instance variables must be -- it is
entirely legal (and quite common on a heavily used application) for multiple
threads to be active inside the same object at the same time.

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to