This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JSP and the fly likes shopping cart prob Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "shopping cart prob" Watch "shopping cart prob" New topic
Author

shopping cart prob

minty hmm
Greenhorn

Joined: Oct 27, 2004
Posts: 1
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!




shop.jsp:
<%@ 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);
%>
<html>
<body>


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

</tr>
<% }
conn.close(); %>

</TABLE>
</body>
</html>







buyItem3.jsp:
<%@ 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" />

<html>
<body>

<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>
<p><input type = "submit" value = "submit" /></p>
<INPUT TYPE=RESET>

</form>

<%
if (item.getproductID()!=0)
{
mycart.addItem(item);
%>
<jsp:forward page = "showcart3.jsp">
<%
}
%>
<a href="showcart3.jsp"> go to next page</a>

</body>
</html>






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

<html>
<body>
<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">

<td><b>pid</font></td></td>
<td><b>pname</font></td></td>
<td><b>Price</font></td></td>
<td><b>Quantity</font></td></td>
</tr>
<%
Enumeration e = mycart.getCart();
float total = 0;
while(e.hasMoreElements()) {
productInfo theItem = (productInfo)e.nextElement();
total += theItem.getunitPrice() * theItem.getstock();
%>
<tr bgcolor="#E9F4FF">
<td><%=theItem.getproductID()%></td>
<td><%=theItem.getproductName()%></td>
<td><%=theItem.getunitPrice()%></td>
<td><%=theItem.getstock()%></td>
</tr>
<%
}
%>
</table>
Total: <%=total %>

</body>
</html>







CartBean2.java:
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);
Cart.put(stringPID,item);
}


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

public void emptyCart() {
Cart = new Hashtable();
}
}








productInfo.java:
package javabeans;

import java.io.*;

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);
}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

"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.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: shopping cart prob
 
Similar Threads
need to transfer productID from url to action class in struts 2
Online Shopping
show cart in shopping cart
Multiple forms in a single JSP page
Indexed Properties Example