Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to set and get values from HashMap using JSTL

Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

When we code in java syntax, we write hashMapRef.put("Key","Value");
How do i set the same thing in JSTL or EL?.

I also want to get the value from the same HashMap back using JSTL or EL. After getting that value i want to see whether that value is blank or not ?

so i want to write an if-else loop i.e.

if(hashMapRef.get("<dynamicKey>") == null)
set value to hashmap i.e. hashMapRef.set("<dynamicKey>")
display something...

I don't want to use scriptlets in JSP. I want to acheive this using JSTL or EL...

Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose if you have the hashmap in the servletrequest as item, you can display the key and value like this.

java.util.HashMap map = new java.util.HashMap();
map.put("key One", "value One");
map.put("key Two", "value Two");
map.put("key Three", "value Three");
map.put("key Four", "value Four");
map.put("key Five", "value Five");

pageContext.setAttribute("map", map);

<c:forEach var='item' items='${map}'>
<c ut value='Key=${item.key}, Value=${item.value}'/>
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also can do something like that:

The output would be:
value One
Posts: 67430
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"RogelWare Garcia",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Forum Bartender
Your mother is a hamster and your father smells of tiny ads!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic