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

Shopping cart session

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to do this :If the user unchecks one or more checkboxes and presses the button "Remove from Basket" then they should be removed from the basket and the "Review Shopping Basket" page updated in this servlet.
Can you help me or provide hints on this?


public class ShoppingCartSessionTracking extends HttpServlet {
public void doGet(
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException {

PrintWriter aPW = response.getWriter();
aPW.println("<HTML><HEAD><TITLE>Shopping Basket using Session Tracking API" +

String actionString = request.getParameter("submit");

if (actionString != null) {
if (actionString.equals("Add More Items")) {
addToShoppingCart(request, aPW);
}else if (actionString.equals("Review Shopping Cart")) {
reviewShoppingCart(request, aPW);
else if (actionString.equals("Remove From Basket")) {
reviewShoppingCart(request, aPW);
}else {
reviewShoppingCart(request, aPW);

private static synchronized void addToShoppingCart (HttpServletRequest request,
PrintWriter aPW) {
aPW.println("<FORM ACTION='ShoppingCartSessionTracking' METHOD=GET>");
aPW.println("Add to Basket:<BR><BR>");
aPW.println("<INPUT TYPE='checkbox' NAME='items' VALUE='Socks $4.0'>Socks $4<BR>");
aPW.println("<INPUT TYPE='checkbox' NAME='items' VALUE='Shoes $30.0'>Shoes $30<BR>");
aPW.println("<BR><BR><INPUT TYPE='Submit' NAME='submit' VALUE='Review Shopping Cart'>");


private static synchronized void reviewShoppingCart(
HttpServletRequest request,
PrintWriter aPW) {

// get current session object. If there is not one create one
HttpSession aHttpSession = request.getSession(true);

// look for vector of items in HttpSession
Vector itemsVec = (Vector)aHttpSession.getAttribute("cart.items");

if (itemsVec == null) {
// if the vector of items is not in the HttpSession create a new Vector
aHttpSession.setAttribute("cart.items", new Vector());
itemsVec = (Vector)aHttpSession.getAttribute("cart.items");

// read in the new items the user wishs to add to their shopping cart
String newItems[] = request.getParameterValues("items");

// if there are new items to add to the shopping cart add them to the itemsVec
if (newItems != null) {
System.out.println("newItems.length = " + newItems.length);
for (int i=0; i<newItems.length; i++) {

// update HttpSession. Old Vector replaced
aHttpSession.setAttribute("cart.items", itemsVec);

aPW.println("Review Shopping Basket<BR><BR>");

if (itemsVec.size() > 0) {
Enumeration aEnumItems = itemsVec.elements();

float count = 0;

while (aEnumItems.hasMoreElements()) {
// retrieves the price from the item description
// so that all the prices of all the items can be added to
// give a total
String details = (String)aEnumItems.nextElement();
int positionOfPound = details.indexOf("$") + 1;
String numberStr = details.substring(positionOfPound);
float price = Float.parseFloat(numberStr);
count = count + price;
aPW.println("<LI>" + details);

aPW.println("<BR><BR>Total: $" + count);
}else {
aPW.println("No items currently in basket");
aPW.println("<FORM ACTION='ShoppingCartSessionTracking' METHOD=GET>");
aPW.println("<INPUT TYPE='checkbox' NAME='items' VALUE='Shirt $16'>Shirt $16<BR>");
aPW.println("<INPUT TYPE='checkbox' NAME='items' VALUE='Trousers $30'>Trousers $30<BR>");
aPW.println("<INPUT TYPE='checkbox' NAME='items' VALUE='Jumper $20'>Jumper $20<BR>");
aPW.println("<BR><BR><INPUT TYPE='Submit' NAME='submit' VALUE='Add More Items'>");
aPW.println("<BR><BR><INPUT TYPE='Submit' NAME='submit' VALUE='Remove from Basket'>");
Posts: 67747
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"best deal",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

JavaRanch Sheriff
    Bookmark Topic Watch Topic
  • New Topic