I know that I can set a list as an attribute with the following code :
List<String> elements = new ArrayList<>();
list.add("Element 1");
list.add("Element 2");
list.add("Element 3");
request.setAttribute( "elementsJSP", elements );
But I would like to send the values to specific indexes, with a code that looks like this but it didn't work :
request.setAttribute( "elementsJSP[2]", "hello" );
request.setAttribute( "elementsJSP[5]", "world" );
request.setAttribute( "elementsJSP[10]", "stackoverflow" );
After that, I would like to parse "elementsJSP" with a "<c:forEach>" loop.
How can I achieve this?
Here is one way. I used an array not a list.
Output: [Element 1, Element 2, Element 3, Element 4, Element 5, Element 6, Element 7, Element 8, Element 9, Element 10, Element 11] [Element 1, Element 2, hello, Element 4, Element 5, world, Element 7, Element 8, Element 9, Element 10, stackoverflow] Element 1 Element 2 hello Element 4 Element 5 world Element 7 Element 8 Element 9 Element 10 stackoverflow
Another way
Same Output.