Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just the same way as you would do in normal Java code.</p> <pre><code>for (Map.Entry&lt;String, String&gt; entry : countries.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); // ... } </code></pre> <hr> <p><strong>However</strong>, <em>scriptlets</em> (raw Java code in JSP files, those <code>&lt;% %&gt;</code> things) are considered a <a href="https://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files/3180202#3180202">poor practice</a>. I recommend to install <a href="http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar" rel="noreferrer">JSTL</a> (just drop the JAR file in <code>/WEB-INF/lib</code> and declare the needed <a href="http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/" rel="noreferrer">taglibs</a> in top of JSP). It has a <a href="http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/c/forEach.html" rel="noreferrer"><code>&lt;c:forEach&gt;</code></a> tag which can iterate over among others <code>Map</code>s. Every iteration will give you a <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.Entry.html" rel="noreferrer"><code>Map.Entry</code></a> back which in turn has <code>getKey()</code> and <code>getValue()</code> methods.</p> <p>Here's a basic example:</p> <pre class="lang-xml prettyprint-override"><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;c:forEach items="${map}" var="entry"&gt; Key = ${entry.key}, value = ${entry.value}&lt;br&gt; &lt;/c:forEach&gt; </code></pre> <p>Thus your particular issue can be solved as follows:</p> <pre class="lang-xml prettyprint-override"><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;select name="country"&gt; &lt;c:forEach items="${countries}" var="country"&gt; &lt;option value="${country.key}"&gt;${country.value}&lt;/option&gt; &lt;/c:forEach&gt; &lt;/select&gt; </code></pre> <p>You need a <code>Servlet</code> or a <code>ServletContextListener</code> to place the <code>${countries}</code> in the desired scope. If this list is supposed to be request-based, then use the <code>Servlet</code>'s <code>doGet()</code>:</p> <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) { Map&lt;String, String&gt; countries = MainUtils.getCountries(); request.setAttribute("countries", countries); request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response); } </code></pre> <p>Or if this list is supposed to be an application-wide constant, then use <code>ServletContextListener</code>'s <code>contextInitialized()</code> so that it will be loaded only once and kept in memory:</p> <pre><code>public void contextInitialized(ServletContextEvent event) { Map&lt;String, String&gt; countries = MainUtils.getCountries(); event.getServletContext().setAttribute("countries", countries); } </code></pre> <p>In both cases the <code>countries</code> will be available in <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html" rel="noreferrer">EL</a> by <code>${countries}</code>.</p> <p>Hope this helps.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/2148658/iterate-over-elements-of-list-and-map-using-jstl-cforeach-tag/">Iterate over elements of List and Map using JSTL &lt;c:forEach&gt; tag</a></li> <li><a href="https://stackoverflow.com/questions/2141464/how-to-iterate-over-a-nested-map-in-cforeach/">How to iterate over a nested map in &lt;c:forEach&gt;</a></li> <li><a href="https://stackoverflow.com/questions/2117557/how-to-iterate-an-arraylist-inside-a-hashmap-using-jstl/">How to iterate an ArrayList inside a HashMap using JSTL?</a></li> <li><a href="https://stackoverflow.com/questions/3468150/using-special-auto-start-servlet-to-initialize-on-startup-and-share-application">Using special auto start servlet to initialize on startup and share application data</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload