1

In a jsp page, a variable is retaining its value after the page is refreshed. I want to assign a value of a variable to zero every time the page refreshes, how to do that?

<%! 
    String s[] = new String[100];
    String s1[] = new String[100];
    int i=0;
 %>
<html>
    <head>
        <s:iterator value="data">
            <% 
                s1[i]=(String)request.getAttribute("build_id");
                s[i]=(String)request.getAttribute("bui_id");
                i++;
            %>
        </s:iterator>
    </head>
</html>

Here my i value should be re-initialized to zero, once my page refreshes.

Lundin
  • 195,001
  • 40
  • 254
  • 396

1 Answers1

0

<% i=0; %> will initialize an instance variable of your servlet class, which is the same for all requests (see this answer). Initialize your variable here instead:

    <s:iterator value="data">
        <% 
            int i = 0; // will be new for every request
            s1[i]=(String)request.getAttribute("build_id");
            s[i]=(String)request.getAttribute("bui_id");
            i++;
        %>
    </s:iterator>
Community
  • 1
  • 1
Uooo
  • 6,204
  • 8
  • 36
  • 63
  • @aru you are welcome. Please also read the linked answer so you understand why this is the case ;-) – Uooo Jun 25 '13 at 06:19