This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Session info getting lost between servlets. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session info getting lost between servlets." Watch "Session info getting lost between servlets." New topic
Author

Session info getting lost between servlets.

marc goroff
Greenhorn

Joined: Jul 12, 2001
Posts: 3
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

Joined: Jan 06, 2001
Posts: 977
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

Joined: Jul 09, 2001
Posts: 76
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");


Rehan Malik
marc goroff
Greenhorn

Joined: Jul 12, 2001
Posts: 3
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

Joined: Jan 06, 2001
Posts: 977
Try to find out if the cookies are disabled in Netscape, this is something that may cause this.
Robert, Blum
Greenhorn

Joined: May 31, 2001
Posts: 6
I have a similar problem, I am trying to access all textfields as an array, how do I access HTML elements as an array.
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session info getting lost between servlets.
 
Similar Threads
How To Convert a Servlet File to a JSP File
error class not found
Shopping cart session
Sessoin
generics in servlets(sessions)