This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes null value in session variable when showing not null in another page! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "null value in session variable when showing not null in another page!" Watch "null value in session variable when showing not null in another page!" New topic
Author

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

Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
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
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What scope are you using when you store the cart into your HttpSession? Request or session scope?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
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

Joined: Sep 03, 2003
Posts: 173
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
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
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.


Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
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
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
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.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
to add what Anselm said:
HttpSession session= request.getSession(true);
and
HttpSession session= request.getSession();
have the same effect


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null value in session variable when showing not null in another page!