Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

shopping cart prob

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm creating a view shopping cart pg that lets users view what they have added into their cart so far. The steps the user would take are:
1. Click and add item from product catalogue pg generated fr database (shop.jsp)
2. THe above step will bring user to buyItem2.jsp which shows details on the product selected and lets user enter the quantity to buy.
3. After submiting, user is taken to shopping cart pg (shopcart2.jsp)However, the problem is when the user adds on other products, the previous products in the shopping cart are replaced with the details of the latest product. How do i ensure that e details of previous products do not get replaced? I've a feeling the prob lies in buyItem3.jsp. The following r my codes...thanx!

<%@ page import="java.sql.*" %>
<%@ page import="common.*" %>

<jsp:useBean id="item" scope="session" class="javabeans.productInfo" />

MSDataBean dbman = new MSDataBean ();
Connection conn = dbman.getConnection ();
String k="business gifts";
Statement stmt = conn.createStatement();
String sql = "select * from productDB where category='"+k+"'";
ResultSet rs = stmt.executeQuery(sql);

<TABLE align="middle" width=78%>
<% while ({%>
<img src="<%=rs.getString("imagePath")%>" alt="gift" align=left />
<%=rs.getString("description")%> </br> Price:S$<%=rs.getFloat("unitPrice")%>
<FORM METHOD="LINK" ACTION="buyItem3.jsp">
<INPUT TYPE="submit" VALUE="Add to cart">

<% }
conn.close(); %>


<%@ page import = "javabeans.*" %>

<%@ page import="common.*" %>

<jsp:useBean id="item" scope="session" class="javabeans.productInfo" />
<jsp:setProperty name="item" property="productID" />
<jsp:setProperty name="item" property="productName" />
<jsp:setProperty name="item" property="unitPrice" />
<jsp:setProperty name="item" property="description" />
<jsp:setProperty name="item" property="category" />
<jsp:setProperty name="item" property="stock" />
<jsp:setProperty name="item" property="imagePath" />

<jsp:useBean id="mycart" scope="session" class="javabeans.CartBean2" />


<form name="addCart" method="POST" action="buyItem3.jsp" >
<h3><align= "center"><jsp:getProperty name="item" property="productName" /></h3>
<IMG src="<jsp:getProperty name="item" property="imagePath" />" align center>
<p><jsp:getProperty name="item" property="description" /> </P>
<P>PriceS$<jsp:getProperty name="item" property="unitPrice" /> per set</p>
<P> Quantity

<input type="text" name="stock" value="" maxlength="2" SIZE="2" />
<input type="hidden" name="productID" />
<input type="hidden" name="productName" />
<input type="hidden" name="unitPrice"/>
<p><input type = "submit" value = "submit" /></p>


if (item.getproductID()!=0)
<jsp:forward page = "showcart3.jsp">
<a href="showcart3.jsp"> go to next page</a>


<%@ page import="javabeans.*, java.util.*" %>
<jsp:useBean id="mycart" class="javabeans.CartBean2" scope="session" />

<h3>Shopping Cart</h3>

Here are the items you have selected:<br>
<table border="0" cellpadding="8" cellspacing="2" width="871" height="40">
<tr bgcolor="#666699">

Enumeration e = mycart.getCart();
float total = 0;
while(e.hasMoreElements()) {
productInfo theItem = (productInfo)e.nextElement();
total += theItem.getunitPrice() * theItem.getstock();
<tr bgcolor="#E9F4FF">
Total: <%=total %>

package javabeans;

import javabeans.*;
import java.util.*;

public class CartBean2 {

private Hashtable Cart;

public CartBean2(){
Cart = new Hashtable();

public Enumeration getCart(){
return Cart.elements();

public void addItem(productInfo item) {
int intPID=item.getproductID();
String stringPID = Integer.toString(intPID);

public void removeItem(productInfo item){
int intremoveID= item.getproductID();
String removeID = Integer.toString(intremoveID);
if(intremoveID != 0) { //trying to remove something from the cart

public void emptyCart() {
Cart = new Hashtable();
package javabeans;


public class productInfo implements Serializable {
int productID;
String productName;
String category;
String description;
float unitPrice;
String imagePath;
int stock;

public int getproductID(){
return productID;
public void setproductID(int aproductID){
productID = aproductID;
public String getproductName(){
return productName;
public void setproductName(String aproductName){
productName = aproductName;
public String getcategory(){
return category;
public void setcategory(String acategory){
category = acategory;
public String getdescription(){
return description;
public void setdescription(String adescription){
description = adescription;
public float getunitPrice(){
return unitPrice;
public void setunitPrice(float aPrice){
unitPrice = aPrice;
public String getimagePath(){
return imagePath;
public void setimagePath(String aimagePath){
imagePath = aimagePath;
public int getstock(){
return stock;
public void setstock(int q){
stock = q;

public String toString(){
return (productID+productName+category+description+unitPrice+imagePath+stock);
Posts: 67618
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
"minty hmm",

We're pleased to have you here with us in the JSP forum, but there are a few rules that need to be followed, and one is that proper names are required. 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.

Forum Bartender
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic