• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

session prob??

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just make it false in the 2nd stage.
HttpSession clientSession = request.getSession( false );
-venu
 
Richard West
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic