my dog learned polymorphism
The moose likes JSP and the fly likes How to set and get values from HashMap using JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to set and get values from HashMap using JSTL" Watch "How to set and get values from HashMap using JSTL" New topic

How to set and get values from HashMap using JSTL

M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
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...


Regards, Manish
SCJP 1.4
Vicky Mohan
Ranch Hand

Joined: Oct 14, 2004
Posts: 130
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}'/>
Rogel Garcia

Joined: Oct 22, 2004
Posts: 4
You also can do something like that:

The output would be:
value One
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

"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

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: How to set and get values from HashMap using JSTL
jQuery in Action, 3rd edition