I have been working with ASP.NET for a few years and am now working on a project using JSP, Struts, and Java so I am fairly new to this.
I have a for-loop in a JavaScript function that looks something like this:
<% int count=0; %>
for(i = 0; i < arrayCount; i++){
jsArray[i] = <%= myBeanArrayList.get(count) %>;
alert("i = " + i + "count = " + count);
<% count++; %>
}
The count variable doesn’t increment even if I use <% count = count + 1 %>. I don’t understand why that piece of code doesn’t do as I want inside the loop. Does anyone have any suggestions on how I can increment the count for the JSP Bean?
You are only looping on the client, not the server. The server code only gets executed once. So, for every iteration of the JavaScript loop, you are using the same value –
myBeanArrayList.get(0). View source to look at the generated HTML code and that will probably help to clarify the problem.Edit: Instead, use server-side code to build a JavaScript array literal. I don’t really know JSP, and my Java is a little rusty, but wouldn’t this work?