Retrieving cookie and array values in JSTL tags

The ${cookie} points to a Map<String, Cookie> with the cookie name as map key and the Cookie object as map value. Every iteration over a Map in <c:forEach> gives you a Map.Entry back which in turn has getKey() and getValue() methods. Your confusion is that the Cookie object has in turn also a getValue() method.

<c:forEach items="${cookie}" var="currentCookie">  
    Cookie name as map entry key: ${currentCookie.key}<br/>
    Cookie object as map entry value: ${currentCookie.value}<br/>
    Name property of Cookie object: ${currentCookie.value.name}<br/>
    Value property of Cookie object: ${currentCookie.value.value}<br/>
</c:forEach>

It’s a Map<String, Cookie> because it allows you easy direct access to cookie value when you already know the name beforehand. The below example assumes it to be cookieName:

${cookie.cookieName.value}

Your list example is by the way invalid. The var should not refer the same name as the list itself.

Leave a Comment