Print an array forwarded from a servlet to a JSP

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

Answers


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.


Need Your Help

When i use my Zend_Db_Table_Abstract with select and join, fetchAll return me one row

php mysql database zend-framework join

I have a problem with my class (extend by Zend_Db_Table_Abstract), it returns with only one row each time with a join and select....