aspose file tools*
The moose likes Servlets and the fly likes establishing a persistent cart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "establishing a persistent cart" Watch "establishing a persistent cart" New topic
Author

establishing a persistent cart

jack lemon
Greenhorn

Joined: Jan 21, 2011
Posts: 1
hi,

i try to create a servlet that set a session attribute called "cart" and use cookie to store the cart size(number of products from cart).
...then i am redirecting(using sendRedirect method) the servlet response to another jsp page(cart.jsp).
ok..cart.jsp display the cookie's value,except other previous 2 page..just when i refresh these pages,it gives the cookie's value.

how to solve this problem?





Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

Hey Jack

Its not at all recommended to store persistent data in Cookies as we must design our applications in such a way that even if we do not get value from cookie, app must be able to function. You are better off creating an object that stores all cart related info and storing it as a session attribute(which you can manipulate in your cart.jsp page).Hope this helps...


SCJP 1.6,Preparing (Tryin to prepare) for scwcd
Jayr Motta
Ranch Hand

Joined: Jul 30, 2010
Posts: 110

Hi Jack,

What Hasha said is true, isn't recommended that you count on Cookies to develop a feature for your software, they may not work in some browsers and i suppose that you want your application runs properly in every kind of enviroments, right? Think on the cookies as a device to improve your features while caching something in the client of your user (like you know him, got it? .. you can say: "hey man .. i know you .. you've been here before john doe, how are you?" to make him more confortable )

About what you're trying to do, if you're trying to pass a value to a jsp and you're not gonna use this anymore we recommend that you use request scope instead, control values/attributes across scopes can become a nightmare. But to you do that in this scenario you've described, you'll need to replace the sendRedirect for HttpServletRequest.getRequestDispatcher("cart.jsp").forward(HttpServletRequest, HttpServletResponse), because the sendRedirect sends to the client a code 301 with the link specified as argument to the method in a header and who does the request is the browser, if you use the RequestDispatcher you'll do this at runtime and your user will not even know that the place he is right now isnt the place he requested.

Hope this helps, if it don't or if you have some other doubt, feel free to ask me .. i'll be pleased to help you!


Feel free to ask me anything!
www.BlackBeltFactory.com/ui#!/ref=jmotta, SCJP 6, OCWCD JEE5, OCE EJB JEE6
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: establishing a persistent cart