Win a copy of Learning Regular Expressions this week in the General Computing 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 ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to set and get values from HashMap using JSTL  RSS feed

 
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>")
else
display something...

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


TIA
 
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}'/>
</c:forEach
 
Greenhorn
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
 
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.

Thanks!
bear
Forum Bartender
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!