aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to retrieve value in JSP