Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to retrieve value in JSP

 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic