This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set and get values from HashMap using JSTL

 
M Sharma
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
 
Vicky Mohan
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
 
Rogel Garcia
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic