This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Unable to retrieve value in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Unable to retrieve value in JSP" Watch "Unable to retrieve value in JSP" New topic
Author

Unable to retrieve value in JSP

Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
I have a bean class name is ParentBean
and my ChildBean class extends ParentBean class
I am trying to add a linkedList to a HashMap and retrieve the values
back it in my JSP.
code Sample
String value ="";
HashMap hm = new HashMap();
while(rs.next())
{
ChildBean cb = new ChildBean();
value =rs.getString(1).trim();
cb.setData(rs.getString(1).trim());
cb.setId(rs.getInt(2));
cb.setFirstName(rs.getString(3).trim());
cb.setLastNamet(rs.getString(4).trim());
ParentBean pb = new ParentBean();
pb.setCommObj(cb);

if(hm.isEmpty() || !(hm.containsKey(value)))
{
LinkedList lList = new LinkedList();
lList.add(pb);
hMap.put(value,lList);
}
else if(hm.containsKey(value))
{
LinkedList ls = (LinkedList)hm.get(value);
if(!(ls.contains(pb)))
{
ls.add(pb);
}
}
}
session.setAttribute("LIST",hm);
And This is how I am trying to retrieve it in my JSP
<%
HashMap hm =(HashMap) session.getAttribute("LIST");


if (hm.size() != 0)
{

for(int i=0; i<hm.size();i++)
{

LinkedList ls= (LinkedList)hm.get(value);
ParentBean pb = (ParentBean)ls.get(i);
ChildBean cb = (ChildBean)pb.getCommObj();

System.out.println(cb.getFirstName());
}
}

%>
And the error message i am getting is cannot resolve symbol variable "value"
It is the variable I am using in my servlet as a key
But I am not sure what i should use in my jsp to get back the key
Thanks in advance
I really appreciate your help
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
Hello,

In the servlet,you are setting the variable 'value' to rs.getString(1).
So in jsp file also u need to use the same String object for 'value' variable to get the maped object.
Else the 'value' varible is not declared and initialezed ..in present code.
Regards
Afroz Ahmed


The value of an idea lies in the usage of it.
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
I changed my jsp code to this and it works now
HashMap hm =(HashMap) session.getAttribute("LIST");
List desc = new ArrayList(hm.keySet());
Collections.sort(desc);
for(Iterator iterator = desc.iterator();iterator.hasNext()
{
String dept = (String)iterator.next();
LinkedList ls = (LinkedList)hm.get(dept);

for(int i=0; i<ls.size();i++)
{
ParentBean pb = (ParentBean)ls.get(i);
ChildBean cb = (ChildBean)pb.getCommObj();

System.out.println(dept);
System.out.println(cb.getFirstName());
System.out.println(cb.getLastName());
}
}
The purpose of using LinkedList within a HashMap was to avoid
duplicate names
for Example
Department FirstName LastName
IT Jeff Toland
IT Tim Taylor
Management abc efd
Management xyz aaa
to this
Department FirstName LastName
IT Jeff Toland
Tim Taylor
Management abc efd
xyz aaa

but I don't know why It is creting duplicate key when i add it to hashmap
I mean it is repeating "IT" twice etc . am I missing something
Please help
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unable to retrieve value in JSP
 
Similar Threads
related to java.util.hashtable
How to identify existing Transaction?
Collection question
Doubt related to HashMap
If two different objects have same hashcode, how HashMap will retrieve these two objects?