| Author |
array in session
|
Malhar Barai
Author
Ranch Hand
Joined: Aug 17, 2001
Posts: 399
|
|
hi all i m making a Shopping Cart & i need to store array of values in session. it does get stored, but at a time i only get to see one value only, even after i run the "FOR" loop. my doGet is as below in doPost I retrieve the values from session . need more info...?? tia MB..  [ December 14, 2002: Message edited by: malhar barai ]
|
Malhar Barai
SOA & Java Book
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
|
|
Could you restate exactly what your requirements are in a more general way? I can't really tell what you are trying to do. Bill
|
 |
Sandeep Jain
Ranch Hand
Joined: Oct 25, 2000
Posts: 124
|
|
Hi , I feel u will get only 1 item back from the existing program as u are not appending the item item_id Every time the request come to the server a item_id is created with 1 element. So what u have to do is , first get the array from session add the new element to it and then put the array back in the session .
|
Try and Try Till u succeed<br /> <br />Sandeep Jain
|
 |
Malhar Barai
Author
Ranch Hand
Joined: Aug 17, 2001
Posts: 399
|
|
Originally posted by William Brogden: Could you restate exactly what your requirements are in a more general way? I can't really tell what you are trying to do. Bill
hi bill the problem is exactly as understood by sandeep. I am able to get only one item in the session. MB thnx anyway
|
 |
Malhar Barai
Author
Ranch Hand
Joined: Aug 17, 2001
Posts: 399
|
|
Originally posted by Sandeep Jain: Hi , I feel u will get only 1 item back from the existing program as u are not appending the item item_id Every time the request come to the server a item_id is created with 1 element. So what u have to do is , first get the array from session add the new element to it and then put the array back in the session .
hi Sandeep You are bang on target. but do i achieve this. what I tried is String[] item_id = (String[])session.getValue("item_id"); item_id[item_id.length+1] = item_value;//new item being stored session.putValue("item_id",item_id); but this isn't wrking fine with me, any changes...?? thnx anyway MB..still
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
|
|
hi bill the problem is exactly as understood by sandeep. I am able to get only one item in the session.
That is NOT what I said. I asked what you are trying to do! A very different question. If you want to keep adding items to the session, this is not the way to go about it. You are trying to use a String array - but arrays can not be expanded. Perhaps you want to use one of the collection classes? Bill
|
 |
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
|
|
I agree with Bill. Use a Vector and your problem should go away -- Also -- rather than storing things as you are, consider creating an Item class that contains price, quantity and description fields. Then store the Items in a Vector held in the HttpSession. Kyle
|
Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
|
 |
Malhar Barai
Author
Ranch Hand
Joined: Aug 17, 2001
Posts: 399
|
|
Originally posted by Kyle Brown: Use a Vector and your problem should go away -- Also -- rather than storing things as you are, consider creating an Item class that contains price, quantity and description fields. Then store the Items in a Vector held in the HttpSession. Kyle
hi kyle I think that would solve my problem. About the Item class...can you shed some more light on that. Do you mean to say I should create an Item class with price,quantity & description & access it by Item.price & so on...??? or have I misunderstood you..?? thnx anyway MB
|
 |
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
|
|
Yes, that is exactly what I am saying. Your code for adding a new item would look like: Item anItem = new Item(price,quantity,desc); Vector items = (Vector) session.getAttribute("items"); items.add(anItem); session.putAttribute("items", items); To get it back; Vector items = (Vector) session.getAttribute("items"); Enumeration enum = items.elements(); while (enum.hasMoreElements()) { Item item = (Item) enum.nextElement(); double price = item.price; String desc = item.desc; int qty = item.qty; // now print them out or whatever } Item would just have the attributes described above, a constructor, and maybe some getters and setters. This is called OBJECT-ORIENTED PROGRAMMING and it's what you're supposed to do in Java Kyle
|
 |
Malhar Barai
Author
Ranch Hand
Joined: Aug 17, 2001
Posts: 399
|
|
thnx a ton kyle & all others who helped me out. MB
|
 |
 |
|
|
subject: array in session
|
|
|