tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: JSP: SingleThreadModel + page variables
Date Sun, 20 Feb 2000 01:14:22 GMT
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

View raw message