Home > Java > javaTutorial > How to Iterate Through a HashMap to Populate a JSP Dropdown List?

How to Iterate Through a HashMap to Populate a JSP Dropdown List?

Barbara Streisand
Release: 2024-12-26 14:21:10
Original
809 people have browsed it

How to Iterate Through a HashMap to Populate a JSP Dropdown List?

Looping Through a HashMap in JSP

Problem Statement

How to effectively iterate through a HashMap within JSP scriptlets to populate a drop-down list?

Solution

In JSP, traversing a HashMap is identical to regular Java code. One can employ the following approach:

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

Using JSTL for Iterations

However, using JSP scriptlets is discouraged. Instead, it's recommended to leverage JSTL's tag, which allows iterating over Maps. Each iteration returns a Map.Entry object with getKey() and getValue() methods.

For instance:

<%@ taglib prefix="c" uri="jakarta.tags.core" %>

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

Implementing the Select Drop-Down

Adapting the aforementioned code to populate a drop-down list:

<%@ taglib prefix="c" uri="jakarta.tags.core" %>

<select name="country">
    <c:forEach items="${countries}" var="country">
        <option value="${country.key}">${country.value}</option>
    </c:forEach>
</select>
Copy after login

Data Placement in Scope

To make countries accessible from within ${countries}, it needs to be placed in the appropriate scope. This can be achieved through:

  • Request scope (Servlet doGet method):

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
      Map<String, String> countries = MainUtils.getCountries();
      request.setAttribute("countries", countries);
      request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
    }
    Copy after login
  • Application-wide scope (ServletContextListener contextInitialized method):

    public void contextInitialized(ServletContextEvent event) {
      Map<String, String> countries = MainUtils.getCountries();
      event.getServletContext().setAttribute("countries", countries);
    }
    Copy after login

Additional Resources

  • [Iterating over Lists and Maps in JSP using JSTL](https://www.journaldev.com/2680/jsp-iterate-over-list-map-jstl)
  • [Iterating over Nested Maps in JSTL](https://stackoverflow.com/questions/11210152/how-to-iterate-over-a-nested-map-in-c-forEach)
  • [Iterating an ArrayList within a HashMap using JSTL](https://stackoverflow.com/questions/13002775/how-to-iterate-an-arraylist-inside-a-hashmap-using-jstl)
  • [Initializing Servlet and Sharing Application Data on Startup](https://www.mkyong.com/servlet/startup-servlet-contextlistner/)

The above is the detailed content of How to Iterate Through a HashMap to Populate a JSP Dropdown List?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template