• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

null value in session variable when showing not null in another page!

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have the following class for shopping cart. Its working fine in storing the objects until i go to submit than i cannot access an Enumeration of cartItems. here is how the class looks like.
class Cart{
private String orderNo;
private Enumeration items;
public void setItems(Vector vc){
this.items= vc.elements();
}
public Enumeration getItems(){
return this.items;
}
}

The vector in setItems is storing CartItem Objects.first form submits to CartServlet, and i take parameters in it, create Cart Object and setOrderNo() on that object, than put it in the session. The user is than forwarded to CartItemsSelection page (where i can easily retrieve the session object). The user clicks on the items needed and submits to CartItemServlet, which takes the params, create a vector of CartItem objects after that retrieve the session cart object and call setItems(vector) on it, the user is taken back to the main OrderPage where the whole order is shown for confirmation.Still on main page i can access the complete Cart Object along with items enumeration in it.
Now when the user submits the record to the next servlet i can access the plain variable from session like orderNo but the getItems() returns a null enumeration.
Which i just accessed in last JSP page and have made no changes to the session object.
I will really appreciate if someone can help me out why this is happening.
Thanks and Regards
Gul
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What scope are you using when you store the cart into your HttpSession? Request or session scope?
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cart= (Cart) session.getAttribute("cart");
cart.setItems(Vector);
session.setAttribute("cart",cart);
these are the lines i m using to retrieve,set and put back items in the session in the ItemsServlet servlets. Before this the cartServlet sets the other attributes by taking request parameters from the form. none of the JSPs are setting the session. they only retrieve and use it.
Thanks for replying ot my post.
Gul
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sort of complete picture of the ItemsServlet
HttpSession session= request.getSession(false);
Cart cart;
cart= (Cart) session.getAttribute("cart");
Vector vc= new Vector();
String productId,qty;
productId= request.getParameter("PID");// same way for qty
// loop through all the selected items and keep adding to vector
vc.addElement(new Item(productId,qty));
cart.setItems(vc);
session.setAttribute("cart",cart);
I can access the Items object(via Enumeration) from session in the next page(confirmation page) along with all the data already put in the session (cart) from the first form. But after it is submitted form confirmation page i can only access tge cart variables but not the Item Object. i.e.
cart.getItems() should return the enumeration on the vector added to it but the enumeration returned contains no elements.
Your reply is very much appreciated. Thanks
Gul
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Looking at the symptoms of your problem I would hazard a guess that something during your order submission process is either clearing the items from your shopping cart or placing a new Cart() object onto your session.
I suggest that you do a bit of debugging or use log4j to see exactly when the items are removed from your Cart.
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i found out where the problem is but dont know Y it is.
I have a line in the jsp page which shows the whole order along with its items. its like this
Cart cart= (Cart) session.getAttribute("cart");
Enumeration cartEnum= cart.getItems();
CartItem item= new CartItem();
while(cartEnum.hasMoreElements()){
item=(CartItem) cartEnum.nextElement();
out.println(item.toString());
}
Now when i iterate through this cartEnum once and try to access it again there are NO elements in it. thats y i m getting nothing in my session object. If i re-put cartEnum into the session before i display(iterate) through it on the page than i do get the values in session BUT than i dont get any items to display.
Its 1.45 am in the morning in my part of the world. Tonite is gone but i would love to sleep tomorrow, possible only if i can solve it.So Plz.. Any Ideas?
Thanks
Gul
 
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pervaiz gul :
Change HttpSession session= request.getSession(false);
to HttpSession session= request.getSession(true);
with the command request.getSession(false); you are basically saying that if the session does not exist(which of course is the case at the first call) do not create it. so no session was created in the first place.
Anselm Paulinus.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to add what Anselm said:
HttpSession session= request.getSession(true);
and
HttpSession session= request.getSession();
have the same effect
 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
I tried that also with request.getSession(true) also with no parameter. the problem as with Enumeration object once i iterate thourgh it i dont find any more elements in it the next time i access it. the rest of the object i can get as it is from session. So i am storing vector in the session rather than the Enumeration. which is working fine with request.getSession(false) also.
Thanks for the reply
Gul
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic