You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Andy Nuss <an...@siebel.com> on 2000/02/20 02:07:54 UTC

JSP: SingleThreadModel + page variables

Hi,

I've read about the variables which persist on a page:

<%! int myvar; %>

and I'm worried that the only way that a typical Web Server can compile this
page successfully to a Servlet is by implementing the SingleThreadModel
interface!  I infer this because it claims somewhere on Sun's site that
these variables are class variables.  Obviously 2 threads using the
same instance of the servlet, each responding to their own GET requests
would be stomping on each others variables unless the SingleThreadModel
is used.

Am I correct?  Is it a requirement that these variables be implemented as
class variables?  Can they be implemented as variables declared at the top
of the scope of the doGet() method?  By a smart Web Server?

Thanks,
Andy


Re: JSP: SingleThreadModel + page variables

Posted by Chris Janicki <Ja...@ia-inc.com>.
I've personally found it a big help to look at the java files created in the work
directory for each jsp.  I was really confused on how everything worked until
I investigated some of those dynamically build java files.  By comparing your jsp
file to the java output, you can more predictably write and debug good jsp code.

Chris


"Craig R. McClanahan" wrote:

> Andy Nuss wrote:
>
> > Hi,
> >
> > I've read about the variables which persist on a page:
> >
> > <%! int myvar; %>
> >
> > and I'm worried that the only way that a typical Web Server can compile this
> > page successfully to a Servlet is by implementing the SingleThreadModel
> > interface!  I infer this because it claims somewhere on Sun's site that
> > these variables are class variables.  Obviously 2 threads using the
> > same instance of the servlet, each responding to their own GET requests
> > would be stomping on each others variables unless the SingleThreadModel
> > is used.
> >
>
> You are correct, although the way to refer to them is instance variables,
> rather than class variables.  Such variables are shared across requests to this
> JSP page -- both simultaneous and subsequent.
>
> Note that SingleThreadModel would solve the first problem, but not the second
> -- the variable would live on as long as this particular page has been loaded.
>
> >
> > Am I correct?  Is it a requirement that these variables be implemented as
> > class variables?  Can they be implemented as variables declared at the top
> > of the scope of the doGet() method?  By a smart Web Server?
> >
>
> If you want a local variable that doesn't get clobbered by concurrent requests,
> just remove the "!" sign and say
>
>     <% int myvar; %>
>
> This variable gets declared inside the doGet()/doPut() method, so it is
> allocated on the stack.
>
> >
> > Thanks,
> > Andy
> >
>
> Craig McClanahan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Re: JSP: SingleThreadModel + page variables

Posted by "Craig R. McClanahan" <cm...@mytownnet.com>.
Andy Nuss wrote:

> Hi,
>
> I've read about the variables which persist on a page:
>
> <%! int myvar; %>
>
> and I'm worried that the only way that a typical Web Server can compile this
> page successfully to a Servlet is by implementing the SingleThreadModel
> interface!  I infer this because it claims somewhere on Sun's site that
> these variables are class variables.  Obviously 2 threads using the
> same instance of the servlet, each responding to their own GET requests
> would be stomping on each others variables unless the SingleThreadModel
> is used.
>

You are correct, although the way to refer to them is instance variables,
rather than class variables.  Such variables are shared across requests to this
JSP page -- both simultaneous and subsequent.

Note that SingleThreadModel would solve the first problem, but not the second
-- the variable would live on as long as this particular page has been loaded.

>
> Am I correct?  Is it a requirement that these variables be implemented as
> class variables?  Can they be implemented as variables declared at the top
> of the scope of the doGet() method?  By a smart Web Server?
>

If you want a local variable that doesn't get clobbered by concurrent requests,
just remove the "!" sign and say

    <% int myvar; %>

This variable gets declared inside the doGet()/doPut() method, so it is
allocated on the stack.

>
> Thanks,
> Andy
>

Craig McClanahan