Reputation: 525
I have forwarded an array userName[][] from a servlet to a JSP. I can access the array elements like ${userName[2][3]}
, but i cant iterate through the array using a variable. e.g. ${userName[i][j]}
or ${userName[<%=i>][<%=j>]}
dont work.
also, should i declare my index variables as var(JS) as my code also uses JS to plot a graph from the array,or do I need to use JSTL? I'm a complete newbie to JSP
Upvotes: 0
Views: 609
Reputation: 691655
Here's how you iterate through an array in JSTL (Note that I pluralized your userName variable, since it's an array):
<c:forEach var="userName" items="userNames">
// do something with the userName
</c:forEach>
Since you array is an array of arrays, you can nest two iterations:
<c:forEach var="innerArray" items="userNames">
<c:forEach var="element" items="innerArray">
// do something with the element
</c:forEach>
</c:forEach>
Note that JavaScript executes at client-side, whereas the JSP is executed at server-side. When JS code executes, it doesn't have access to your server-side Java array. If you need to access the contents of the Java array at client-side, you should serialize it with JSON, and parse the resulting JSON String in JavaScript.
Upvotes: 1