Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaration section variable is not getting reset for each refresh operation?

 
yamini nadella
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<HTML><BODY>
Hello 1
<B><%!
int i=10; // --- 1
public void b()
{
i = i +5;
}
public void a()
{
b();
i = i+10;
}
%></B>

Hello 2
<B><%
a();
out.print("first line "+i);
%></B>
</BODY></HTML>

output of above script is looking like below.

Hello 1 Hello 2 first line 25
Upto this it is looking fine. But when I clicked refresh button of Internet explorer then I got
Hello 1 Hello 2 first line 40
After pressing refresh button.
Hello 1 Hello 2 first line 55
After another refresh button.
Hello 1 Hello 2 first line 70

why I is acting like a static variable. is all variables declared in declaration tags (<%! %> acts like this.
How to make these variables to act dynamically? I mean whenever I click refresh then it has to show same values.

What is the scope of this variable is called ?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the declaration directive, it creates declarations! Big surprise. So, these are actually instance variables that are created. That is why you are seeing the value continually increase. If you want a value that is "new" for each request, you will want to define your variable in a scriptlet.

Or, you can stop using scriptlets altogether and find a better way to do all this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic