This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

In JSP : Retrieve data from a hashmap  RSS feed

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a bean in my jsp and one of it's properties is a hashmap of objects. I'd like to go over the hashmap and check whether a particular key exists.
Struts is also involved here, but I do not believe
that Struts is causing any problems.

The key/values are placed in the hashmap as follows:
hMap = new HashMap(20);
while ( ) {
fRoles = new FormRoles();
fRoles.setCreate( ( rs.getInt("C")==1)? true:false );
fRoles.setRead( ( rs.getInt("R")==1)? true:false );
fRoles.setUpdate( ( rs.getInt("U")==1)? true:false );
fRoles.setDelete( ( rs.getInt("D")==1)? true:false );
hMap.put(rs.getString("FormName"), fRoles);
} //while
user.setFormRoles( hMap);
In the above code I place the FormRoles objects into the hashmap with
a String as the key. I should be able to retrieve the objects with a
String literal. Ie. if the form name is "manager", get("manager") will return the corresponding FormRole object. I should be able to say containsKey("manager") and get "true". That is not what's happening:

Here is my JSP:

<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="c_rt" uri="" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>



<title>Welcome World!</title>



<body bgcolor="white">
<logic resent scope="session" name="user">
<h3>Welcome <bean:write name="user" property="userId"/>!</h3>
<bean:write name="user" property="userFName"/>
<bean:write name="user" property="userLName"/>
</logic resent>
<logic:notPresent scope="session" name="user">
<h3>Welcome World!</h3>


<li><html:link forward="logon">Sign in</html:link></li>
<logic resent scope="session" name="user">
<li><html:link forward="logoff">Sign out</html:link></li>
<bean efine id="fRoles" name="user" property="frmRoles"
type="java.util.HashMap" toScope="page"/>
<logic:iterate id="element" name="fRoles">
Key is <bean:write name="element" property="key"/> <br>
Value is <bean:write name="element" property="value"/> <br>

<c:forEach var="element" items="${fRoles}">
<c ut value="Key=${element.key}, Value=${element.value}"/>

fRoles.size() returns <%= fRoles.size() %> <br>
fRoles.keySet() returns <%= fRoles.keySet() %> <br>
fRoles.containsKey("manager") returns <%=
fRoles.containsKey("manager") %>
<c:forEach items="${fRoles}" var="item">
<c:if test="${item.key == 'manager'}">
Found manager

<c:when test= '<% fRoles.get("manager") != null %> '>
Found formRoles manager
<c therwise>
NOT Found
</c therwise>

</logic resent>
<img src='struts-power.gif' alt='Powered by Struts'>

Here is the resulting page:

Welcome BC!


* Sign in
* Sign out

Key is country
Value is com.parkerglobal.mgrappBusiness.FormRoles@a2e19e
Key is manager
Value is com.parkerglobal.mgrappBusiness.FormRoles@18a62f6
Key is region
Value is com.parkerglobal.mgrappBusiness.FormRoles@6742d0
Key is currency
Value is com.parkerglobal.mgrappBusiness.FormRoles@1539d49
Key is classificationType
Value is com.parkerglobal.mgrappBusiness.FormRoles@7b5cb8
Key is managerFundClass
Value is com.parkerglobal.mgrappBusiness.FormRoles@1046270
Key is pgsPortfolio
Value is com.parkerglobal.mgrappBusiness.FormRoles@dad4b8
Key is pgsMgrAllocations
Value is com.parkerglobal.mgrappBusiness.FormRoles@c39410
Key is pgsPortfolioShareClass
Value is com.parkerglobal.mgrappBusiness.FormRoles@2db73e
Key is managerFund
Value is com.parkerglobal.mgrappBusiness.FormRoles@1594ba3
Key is classification
Value is com.parkerglobal.mgrappBusiness.FormRoles@174f478
Key=country , Value=com.parkerglobal.mgrappBusiness.FormRoles@a2e19e
Key=manager , Value=com.parkerglobal.mgrappBusiness.FormRoles@18a62f6
Key=region , Value=com.parkerglobal.mgrappBusiness.FormRoles@6742d0
Key=currency ,
Key=classificationType ,
Key=managerFundClass ,
Key=pgsPortfolio ,
Key=pgsMgrAllocations ,
Key=pgsPortfolioShareClass ,
Key=managerFund ,
Key=classification ,
fRoles.size() returns 11
fRoles.keySet() returns [country , manager , region , currency ,
classificationType , managerFundClass , pgsPortfolio ,
pgsMgrAllocations , pgsPortfolioShareClass , managerFund ,
classification ]
fRoles.containsKey("manager") returns false
NOT Found

This tells me that I have a valid hashmap object. Notice that fRoles.containsKey("manager") return false. Why???
Also notice "NOT Found" that comes from <c:when test= '<% fRoles.get("manager") != null %> '>.
Someone on google suggested that there are trailing white spaces. So made sure I had fRoles.setFormName(rs.getString("FormName").trim());
I also tried when test with "manager ".
Nothing works. I'd appreciate if you could help me.

Thanks in advance.

Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the key. It may be "manager "

As I see from your code you are doing trim while creating object

While putting object in the hashmap you are not doing trim.
Is it possible key used for get may be trimmed?

Nadia Kunkov
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!!!
That was it. I didn't have trim() when adding it to a hashmap.
You have saved me a lot of grieve.
Appreciate your help.
Thanks again.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!