• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add several item to HashMap?

 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add more than one item to HashMap When I click the "submit" and I want to made a Shopping cart.But I can't add several item to the HashMap.It will clear the content that I added to it When I click the second time.Anyone have good idea?
Mycode is://///////////////////////////////////////////////////////////////////
<select name="item" size="3" multiple>
<option value="jsp">jsp</option>
<option value="asp">asp</option>
<option value="PHP">PHP</option>
</select>
</p>
<input type="text" name="itemnumber">
<p>
<input type="submit" name="Submit" value="submit">
</p>
</form>
<%
String item="asp";
item=request.getParameter("item");
String itemnum="1";
itemnum=request.getParameter("itemnumber");
int itemn=(Integer.valueOf(itemnum)).intValue();
out.println("Your select is:"+item);
out.println("Item number is:"+itemnum);
HashMap cart;
cart=new HashMap();
cart.put(item,new Integer(itemnum));
out.println("Your cart is:"+cart.entrySet());
%>
<HR>
<table border="1" bordercolor="red">
<%
String str="";
out.println("your select:");
str+="<tr><td>"+cart.entrySet()+"</td></tr>";
out.println(str);
%>
Thank you!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a multiple select choice box, you get several parameters with the same name. But getParameter only returns the first one.
Look at the request method getParameterValues( name ) - it returns a String array with size equal the number of chosen options.
A hashmap will only store one object per name but you could store the array.
Since you make a new HashMap every time, it is no surprise that the old one is lost.
Bill
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right!Thank you.It works well after I modified it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!