aspose file tools*
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
Author

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

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


TIA


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}'/>
</c:forEach
Rogel Garcia
Greenhorn

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

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

Joined: Jan 10, 2002
Posts: 61788
    
  67

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set and get values from HashMap using JSTL