• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

Java DataStructure

I need to be able to pass a table with 2 values from a servlet to a JSP page.  I'm have trouble getting hashmap or List to work.  Any suggestions?

Data looks like this!
1 Dog
2 Cat
3 Pig
4 Goat
....
Thanks,
Brian
0
Kcpl
Asked:
Kcpl
1 Solution
 
gaston_acevedoCommented:
the sintax for use a Map is:
Map data = new HashMap();
data.put(new Integer(1),"Dog");
data.put(new Integer(2),"Cat");
data.put(new Integer(3),"Pig");
data.put(new Integer(4),"Goat");

anyway, plz, tell me the error you get
0
 
KcplAuthor Commented:
The problem isn't loading the map.  it is getting the map from the attributes.
Java Code:
hashMap = new HashMap();
for(int i = 0; i < categs.length; i++)
{
   hashMap.put( categs[i], i ); // adding value into HashMap
}
request.setAttribute("CATEG", hashMap);
System.out.println("HashMap contains " + hashMap.size() + " key value pair."); 
 System.out.println(hashMap);
 
In tomcat I get the contents of the Hash map with 9 key value pairs
 
JSP
HashMap hashMap = (HashMap) request.getAttribute("CATEG");
if (hashMap != null)
{
   if( hashMap.containsValue( new Integer(1) ) )
     {
         System.out.println("HashMap");
    }
else
{
   System.out.println("HashMap not working!");
}
I get HashMap not working.
Thanks,
Brian

Open in new window

0
 
objectsCommented:
what type is the categs array?

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
KcplAuthor Commented:
private String [] categs;
0
 
rrzCommented:
>request.setAttribute("CATEG", hashMap);
>HashMap hashMap = (HashMap) request.getAttribute("CATEG");  
I think you should try session scope.  The request object in the servlet is not the same request object in the JSP.
try
in Servlet
HttpSession session = request.getSession();
session.setAttribute("CATEG", hashMap);
in JSP
HashMap hashMap = (HashMap) session.getAttribute("CATEG");  
0
 
rrzCommented:
Is this same question?
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/Servlets/Q_24355699.html
If so, then ignore  my suggestion.  If you forwarding then the request should be the same.
0
 
rrzCommented:
Please try this demo JSP .  The two objects are not the same.
<%@ page import="java.util.*" %>
<%
  Integer firstObject = new Integer(1);
  Integer secondObject = new Integer(1);
  HashMap myMap = new HashMap();
  myMap.put(1,firstObject);
  if(myMap.containsValue(firstObject))out.print("first yes <br/>");
  if(myMap.containsValue(secondObject))out.print("second no");
%>

Open in new window

0
 
rrzCommented:
I was wrong.  Sorry I didn't test enough.  Evidently the containsValue method is smarter than I am.  
<%
  Integer firstInteger = new Integer(1);
  Integer secondInteger = new Integer(1);
  String firstString = new String("test");
  String secondString = new String("test");
  HashMap myMap = new HashMap();
  myMap.put(1,firstInteger);
  myMap.put(2,firstString);
  if(!(firstInteger == secondInteger))out.print("Integers not equal <br/>");
  if(!(firstString == secondString))out.print("Strings not equal <br/>");
  if(myMap.containsValue(firstInteger))out.print("first Integer yes <br/>");
  if(myMap.containsValue(secondInteger))out.print("second Integer yes <br/>");
  if(myMap.containsValue(firstString))out.print("first String yes <br/>");
  if(myMap.containsValue(secondString))out.print("second String yes <br/>");
%>

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now