Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session info getting lost between servlets.

 
marc goroff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement a shopping cart. I have servlet which adds items to a cart, and then sends a redirect to another servlet to display the contents of the cart. If I test this using IE, it works just fine, when I test it using Netscape 4.7, the cart is always empty.
Code this adds to the cart:
HttpSession session = request.getSession(true);

if ( session != null ) {

Vector basket = (Vector)session.getValue("basket");

if ( basket == null ) {
basket = new Vector(5);
session.putValue("basket", basket);
}
// Add the passed in frame to the basket.
basket.addElement(frame);
}
**********************************************************
this reads out of the cart
HttpSession session = request.getSession(true);
basket = (Vector)session.getValue("basket");
 
Marcos Maia
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note this:
The getSession method of the HttpServletRequest object returns a user's session. When you call the method with its create argument as true, the implementation creates a session if necessary.
To properly maintain the session, you must call getSession before any output is written to the response. (If you respond using a Writer, then you must call getSession before accessing the Writer, not just before sending any response data.)
 
Rehan Malik
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please also note, putValue has been deprecated and is not recommended for future development.
Use instead "setAttribute" and "getAttribute" to set and get the values in the session.
Also, you have to most likely update the attribute if you add more items to the basket.

Originally posted by marc goroff:
I am trying to implement a shopping cart. I have servlet which adds items to a cart, and then sends a redirect to another servlet to display the contents of the cart. If I test this using IE, it works just fine, when I test it using Netscape 4.7, the cart is always empty.
Code this adds to the cart:
HttpSession session = request.getSession(true);

if ( session != null ) {

Vector basket = (Vector)session.getValue("basket");

if ( basket == null ) {
basket = new Vector(5);
session.putValue("basket", basket);
}
// Add the passed in frame to the basket.
basket.addElement(frame);
}
**********************************************************
this reads out of the cart
HttpSession session = request.getSession(true);
basket = (Vector)session.getValue("basket");

 
marc goroff
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might have been helpful to mention this before, I use the following call to transfer to the new servlet.
response.sendRedirect( response.encodeRedirectUrl( "/servlets/ShoppingBasketServlet" ) );
Anyway, I am not too worried about the code, I just wonder why it does NOT work on Netscape, but does on IE. I realize the (true) argument causes a new session to be created, that is the problem. Why is the session null. What would/could cause it to be lost.
 
Marcos Maia
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to find out if the cookies are disabled in Netscape, this is something that may cause this.
 
Robert, Blum
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a similar problem, I am trying to access all textfields as an array, how do I access HTML elements as an array.
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic