I have a page of the form:
== Start ==
<%! int someVariable = 0; %>
<%= someVariable %>
<% someVaribale++; %>
== End ==
Now when I run the page the first time I get 0 then each subsequent
time a higher number... Why? I should think the scope of someVariable
is local and to make the scope application or session I would need to
do something special. I even clear the cache on firefox and the bloody
thing still increments.
I'm using Netbeans 6.7.1 with Glassfish v3 if it makes a difference.
|
|
0
|
|
|
|
Reply
|
ken473 (54)
|
12/9/2009 9:52:25 PM |
|
Ken wrote:
> I have a page of the form:
>
> == Start ==
>
> <%! int someVariable = 0; %>
> <%= someVariable %>
> <% someVaribale++; %>
>
> == End ==
> Now when I run the page the first time I get 0 then each subsequent
> time a higher number... Why? I should think the scope of someVariable
> is local and to make the scope application or session I would need to
> do something special. I even clear the cache on firefox and the bloody
> thing still increments.
"<%!" opens a declaration block, so 'someVariable' is an instance variable of
the servlet. Servlet containers like to reuse servlet instances whenever
feasible and permitted. So you have the same instance with the same member
incrementing.
Don't use scriptlet in JSP.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
12/10/2009 12:13:48 AM
|
|
Ken wrote:
> I have a page of the form:
>
> == Start ==
>
> <%! int someVariable = 0; %>
> <%= someVariable %>
> <% someVaribale++; %>
>
> == End ==
> Now when I run the page the first time I get 0 then each subsequent
> time a higher number... Why? I should think the scope of someVariable
> is local and to make the scope application or session I would need to
> do something special. I even clear the cache on firefox and the bloody
> thing still increments.
>
> I'm using Netbeans 6.7.1 with Glassfish v3 if it makes a difference.
"<%!" starts a class-level declaration. so someVariable is declared at
the JSP servlet object level. you could use <% int someVariable = 0;
%>. The better alternative is to not use scriptlets at all, and use
some form of MVC architecture.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
|
|
0
|
|
|
|
Reply
|
Daniel
|
12/10/2009 12:18:10 AM
|
|