How to display HashMap Key in jsp/jstl

java, javascript, jsp, jstl

Solution

This is what you need to iterate the Map in JSP. For more info have a look at JSTL Core c:forEach Tag.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${currentLoggedInUsersMap}" var="entry">
    Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>

It's just like a Map.Entry that is used in JAVA as shown below to get the key-value.

for (Map.Entry<String, String> entry : currentLoggedInUsersMap.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
}

Read detained description here on How to loop through a HashMap in JSP?

Problem

I am new to JSP/JSTL. I have set a HashMap on request as follows ``` HashMap <String, Vector> hmUsers = new HashMap<String, Vector>(); HashMap hmUsers = eQSessionListener.getLoggedinUsers(); request.setAttribute("currentLoggedInUsersMap", hmUsers); ``` I am alerting HashMap in My jsp as follows ``` <script> alert("<c:out value = '${currentLoggedInUsersMap}' />"); </script> ``` All works as per my expectations till now. But if I try to get key of this HashMap as follow then nothing is alerted. ``` <script> alert("<c:out value = '${currentLoggedInUsersMap.key}' />"); </script> ``` Is there anything I am going wrong? Thanks in advance.

Original source

Related problems