aspose file tools*
The moose likes Servlets and the fly likes session prob?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session prob??" Watch "session prob??" New topic
Author

session prob??

Richard West
Greenhorn

Joined: Jul 11, 2001
Posts: 3
hello,
i have made a simple shopping cart which goes through the
following stage:
1st stage:
// Additions to the shopping cart
HttpSession clientSession = request.getSession( true );
ShoppingCart vBasket = (ShoppingCart)clientSession.getValue("thecart");
if ( vBasket == null ){
System.out.println("NO CART");
vBasket = new ShoppingCart();
clientSession.putValue("thecart", vBasket);
}
System.out.println("MADE THE CART");

vBasket.add(the_item);
// at this stage, the items are being added to a hashtable

2nd stage:

HttpSession clientSession = request.getSession( true );
System.out.println("GOT THE SESSION");
ShoppingCart vBasket = (ShoppingCart)clientSession.getValue("thecart");
if ( vBasket == null ){
System.out.println("CART HASN'T BEEN MADE!!");
throw new Exception();
}

// At this stage, the object of the ShoppingCart class still has null value even if in the existing session the ShoppingCart has been made at stage 1. Any idea why this is happening??

thanks
Richard
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
You are probably getting a new session.
HttpSession clientSession = request.getSession( true );
The true says to create a new session if one is not found. After the first contact, use false and check clientSession vrs null.
Bill
------------------
author of:
Venugopal nandikolla
Greenhorn

Joined: Feb 02, 2001
Posts: 22
just make it false in the 2nd stage.
HttpSession clientSession = request.getSession( false );
-venu
Richard West
Greenhorn

Joined: Jul 11, 2001
Posts: 3
thanks!
bill and venu for your help.
the thing is i had tried using --
HttpSession clientSession = request.getSession( false );
-- which returns the existing session, and then
checked the session which was not showing null. But after that
an exception is thrown. (the if condition is not reached in the second
stage). also NullPointerException has been taken care of.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
Looks like something about your setup is not maintaining the session information - or - there is something wrong with the way you are addressing it.
1. Browser not keeping cookie - would cause null session on 2nd request.
2. problem with session attribute naming convention
Try this - dump every attribute in the session every time you get it. The HttpSession method getAttributeNames returns an Enumeration - just write out all those names to see what is really in there.
Both of the "stages" you are talking about are in the same servlet, right?
Bill
Richard West
Greenhorn

Joined: Jul 11, 2001
Posts: 3
hi Bill,
somebody suggested to me that
if u declare the writer before u get the
session there may have problem like this.
Both the stages are different servlets. Earlier
I was using this code:
....
response.setContentType("text/html");
out = response.getWriter();
HttpSession clientSession = request.getSession( false );
......
now i am initializing the writer after getting the session and the servlet works fine. I still don't know why the error
happened.
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session prob??