What if you have something like a collection of collections? An array of arrays? You can nest <c:forEach> tags for more complex table structures. In this example, we put String arrays into an ArrayList, then make the ArrayList a request attribute. The JSP has to loop through the ArrayList to get each String array, then loop through each String array to print the actual elements of the array.
Servlet code
String[] movies1 = {"Matrix Revolutions", "Kill Bill", "Boondock Saints"}; String[] movies2 = {"Amelie", "Return of the King", "Mean Girls"}; java.util.List movieList = new java.util.ArrayList(); movieList.add(movies1); movieList.add(movies2); request.setAttribute("movies", movieList);
JSP code
There are no Dumb Questions
The “var” variable is scoped to ONLY the tag!
That’s right, tag scope. No this isn’t a full-fledged scope to which you can bind attributes like the other four—page, request, session, and application. Tag scope simply means that the variable was declared INSIDE a loop.
And you already know what that means in Java terms. You’ll see that for most other tags, a variable set with a “var” attribute will be visible to whatever scope you specifically set (using an optional “scope” attribute), OR, the variable will default to page scope.
So don’t be fooled by code that tries to use the variable somewhere BELOW the end of the <c:forEach> body tag!
It might help to think of tag scope as being just like block scope in plain old Java code. An example is the for loop you all know and love: