Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

array in session  RSS feed

 
Author
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Malhar Barai
Author
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Malhar Barai
Author
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx a ton kyle & all others who helped me out.
MB
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!