{"id":35,"date":"2009-08-31T22:05:47","date_gmt":"2009-08-31T22:05:47","guid":{"rendered":"http:\/\/localhost\/tfcom_wp\/2009\/08\/31\/iterate-map-jstl-el\/"},"modified":"2009-08-31T22:05:47","modified_gmt":"2009-08-31T22:05:47","slug":"iterate-map-jstl-el","status":"publish","type":"post","link":"https:\/\/www.tech-freaks.com\/java\/code-snippets\/iterate-map-jstl-el.html","title":{"rendered":"Iterating a Map using JSTL\/EL"},"content":{"rendered":"<div style=\"text-align: left;\">Normally, we iterate through a list (like ArrayList, LinkedList etc) in JSP. For map (HashMap, TreeMap etc), we would generally be picking up &#8216;value&#8217; based on the &#8216;key&#8217;.<\/div>\n<div style=\"text-align: left;\">\u00a0<\/div>\n<div style=\"text-align: left;\">However, there are situations in which we would like to iterate all the elements of a map and display its key and value. For example, display a list of attribute name and value of a guitar! Each attribute name of the guitar represented as a key and the attribute value as a value. The key value pair would be something like this (&#8216;Type&#8217;, &#8216;Electric&#8217;), (&#8216;Brand&#8217;, &#8216;Gibson&#8217;) etc.<\/div>\n<div style=\"text-align: left;\"><strong>\u00a0<\/strong><\/div>\n<div style=\"text-align: left;\">\n<div>\n<pre class=\"language-markup\"><code>  ....\r\n  .... \r\n  &lt;c:forEach var=\"item\" items=\"${itemsMap}\"&gt;\r\n${item.key} --&gt;  ${item.value} &lt;\/br&gt;\r\n  &lt;\/c:forEach&gt;\r\n  ....<\/code><\/pre>\n<\/div>\n<\/div>\n<div style=\"text-align: left;\">&#8216;itemsMap&#8217; would be a type of Map which is in request scope.\u00a0<\/p>\n<p>The below code is an excerpt from a servlet which adds &#8216;itemsMap&#8217; in request scope and forwards to the JSP which iterates the Map.<\/p><\/div>\n<div style=\"text-align: left;\">\n<pre class=\"language-java\"><code>public void service(HttpServletRequest request, HttpServletResponse response)\r\n   throws ServletException, IOException {\r\n  Map map = new HashMap();\r\n  map.put(\"Type\", \"Electric\");\r\n  map.put(\"Brand\", \"Gibson\");\r\n  map.put(\"Model\", \"Les Paul Black Beauty\");\r\n  request.setAttribute(\"itemsMap\", map);\r\n  \r\n  request.getRequestDispatcher(\"\/DisplayMap.jsp\").forward(request, response);\r\n }<\/code><\/pre>\n<\/div>\n<div style=\"text-align: left;\">One thing to worry about while iterating this way is the ordering of elements. Ensure to use a LinkedHashMap instead of a HashMap, if the order of the elements to be displayed is important.\u00a0<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Normally, we iterate through a list (like ArrayList, LinkedList etc) in JSP. For map (HashMap, TreeMap etc), we would generally [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-code-snippets"],"_links":{"self":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":0,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}