购物车对象是暂时存放商品编号及商品数据,这些数据并不对数据库进行修改,只是一种提示,主要用到一个叫作Hashtable类中的一些方法来进行操作;其中代码中的DB类是用到前面做的一个JAVABEAN来对数据库进行操作的。这里就不重复,我在博客文章里有发表过。里面所用到的数据库不必完全了解,只需要知道是如何对其进行操作,而重点还是在对购物车是如何使用做个了解,在这个购物车里面总共涉及到6个文件BuyCar.java(用于暂时保存数据),buyGoods.jsp(显示所有商品),buyGoods2.jsp(看到购物车),addGoods.jsp(增加购物数量),minusGoods.jsp(减少购物数量),deletGoods.jsp(删除购物数量)
注意,在buyGoods2.jsp,addGoods.jsp,minusGoods.jsp,deletGoods.jsp使用到userBean,同一个类BuyCar,里面id命名应该相同,同时别忘了设置scope="session",因为这个类在这几个文件中进行会话。
现在我们来看看代码
BuyCar.java
package classes;
import java.io.*;
import java.util.*;
public class BuyCar implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8544271003129903410L;
Hashtable myGoods =new Hashtable();
public BuyCar(){}
//把商品加入购物车
public void addGoods(String goods_id,int good_Count){
if(myGoods.containsKey(goods_id)){
int temp_Count = ((Integer)myGoods.get(goods_id)).intValue();
temp_Count= temp_Count+good_Count;
myGoods.put(goods_id, new Integer(temp_Count));
}
else{
myGoods.put(goods_id, new Integer(good_Count));
}
}
//把商品从购物车中拿出来
public boolean minusGoods(String goods_id,int goods_Count){
if(myGoods.containsKey(goods_id)){
int temp_Count = ((Integer)myGoods.get(goods_id)).intValue();
temp_Count= temp_Count-goods_Count;
if(temp_Count<=0){
deleteGoods(goods_id);
}
else{
myGoods.put(goods_id, new Integer(temp_Count));
}
return true;
}
else{
return false;
}
}
//从购物车删除一件物品
public boolean deleteGoods(String goods_id) {
// TODO 自动生成方法存根
if(myGoods.remove(goods_id)==null){
return false;
}
else{
return true;
}
}
//得到购物车中的有商品
public Hashtable listMyGoods(){
return myGoods;
}
}
buyGoods.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="db" class="classes.DB"></jsp:useBean>
<html>
<body>
我要<a href="buyGoods2.jsp">查看购物车</a><br>
<%
String driverName="org.gjt.mm.mysql.Driver";
String jdbcURL="jdbc:mysql://localhost/test";
String userName="root";
String password="198410";
String sql="select * from product";
ResultSet rs=null;
try{
db.setConnection(driverName,jdbcURL,userName,password);
db.setSQL(sql);
rs=db.executeQuery();
out.print("<table border>");
out.print("<tr><td colspan=5 align=center>产品有:</td></tr>");
out.print("<tr>");
out.print("<td width=40>"+"序号");
out.print("<td width=400>"+"产品名");
out.print("<td width=400>"+"产品类型");
out.print("<td width=100>"+"价格");
out.print("<td width=80>"+"购买吗?");
out.print("</tr>");
while(rs.next()){
out.print("<tr>");
out.print("<td>"+rs.getLong(1)+"</td>");
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getFloat(6)+"</td>");
out.print("<td><a href='buyGoods2.jsp?productID="+rs.getLong(1)+"'>我要购买</a></td>");
out.print("</tr>");
}
out.print("</table>");
db.close();
}
catch(SQLException e){
e.printStackTrace();
out.println("执行不成功");
}
%>
</body>
</html>
buyGoods2.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<jsp:directive.page import="java.sql.ResultSet"/>
<jsp:directive.page import="java.util.Hashtable"/>
<jsp:directive.page import="java.util.Enumeration"/>
<jsp:useBean id="buyCar" class="classes.BuyCar" scope="session"></jsp:useBean>
<jsp:useBean id="db" class="classes.DB" scope="session"></jsp:useBean>
<html>
<body>
我要<a href="buyGoods.jsp">我要继续购买</a><br>
<%
String driverName="org.gjt.mm.mysql.Driver";
String jdbcURL="jdbc:mysql://localhost/test";
String userName="root";
String password="198410";
ResultSet rs=null;
db.setConnection(driverName,jdbcURL,userName,password);
if(request.getParameter("productID")==null||(request.getParameter("productID").trim()).length()==0){}
else{
int productID=Integer.parseInt(request.getParameter("productID"));
String sql="select * from product where productID="+productID;
out.print("<table border>");
out.print("<tr><td colspan=5 align=center>你要买的商品是:</td></tr>");
out.print("<tr>");
out.print("<td width=40>"+"序号");
out.print("<td width=400>"+"产品名");
out.print("<td width=400>"+"产品类型");
out.print("<td width=100>"+"价格");
out.print("<td width=80>"+"数量");
out.print("</tr>");
db.setSQL(sql);
rs=db.executeQuery();
out.print("<tr>");
out.print("<td>"+productID);
rs.next();
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getFloat(6)+"</td>");
out.print("<form action=addGoods.jsp method=post>");
out.print("<td><input type=input name=product_count size=6>");
out.print("<input type=hidden name=productID value="+productID+">");
out.print("<input type=submit name=productID value=提交></td></form>");
out.print("</tr>");
out.print("</table>");
}
//列出已放入购物车的商品
double all_price=0;//总价
out.print("<table border>");
out.print("<tr><td colspan=8 align=center>你购物车中的商品有:</td></tr>");
out.print("<tr>");
out.print("<td width=40>"+"序号");
out.print("<td width=400>"+"产品名");
out.print("<td width=400>"+"产品类型");
out.print("<td width=100>"+"价格");
out.print("<td width=80>"+"数量");
out.print("<td width=80>"+"总价");
out.print("<td width=400>"+"删除");
out.print("<td width=80>"+"减少");
out.print("</tr>");
Hashtable list=buyCar.listMyGoods();
Enumeration enum1=list.keys();
while(enum1.hasMoreElements()){
String product_id=(String)enum1.nextElement();
byte[] b=product_id.getBytes("ISO-8859-1");
product_id=new String(b);
String sql1="select * from product where productID="+product_id;
db.setSQL(sql1);
rs=db.executeQuery();
out.print("<tr>");
out.print("<td>"+product_id+"</td>");
rs.next();
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getFloat(6)+"</td>");
out.print("<td>"+list.get(product_id)+"</td>");
out.print("<td>"+rs.getFloat(6)*((Integer)list.get(product_id)).intValue()+"</td>");
all_price=all_price+rs.getFloat(6)*((Integer)list.get(product_id)).intValue();
out.print("<td><a href='deletGoods.jsp?product_id="+product_id+"'>从购物车中删除</a></td>");
out.print("<form action=minusGoods.jsp method=post>");
out.print("<td><input type=input name=product_count size=6>");
out.print("<input type=hidden name=product_id value="+product_id+">");
out.print("<input type=submit value=提交></td></form>");
out.print("</tr>");
}
out.print("<tr>");
out.print("<td colspan=5 align=center>总价为:</td>");
out.print("<td>"+all_price+"</td><td> </td><td> </td>");
out.print("</tr>");
out.print("</table>");
db.close();
%>
</body>
</html>
addGoods.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<jsp:useBean id="buyCar" class="classes.BuyCar" scope="session"></jsp:useBean>
<html>
<body>
<%
String product_id=request.getParameter("productID");
try{
int product_count=Integer.parseInt(request.getParameter("product_count"));
buyCar.addGoods(product_id,product_count);
out.println("成功放入购物车!");
}
catch(Exception e){
out.print(e);
out.print("输入的数字不正确!");
}
%>
我要<a href="buyGoods.jsp">继续购物</a>
我要<a href="buyGoods2.jsp">查看购物车</a>
</body>
</html>
minusGoods.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<jsp:useBean id="buyCar" class="classes.BuyCar" scope="session"></jsp:useBean>
<html>
<body>
<%
String product_id=request.getParameter("product_id");
try{
int product_count=Integer.parseInt(request.getParameter("product_count"));
buyCar.minusGoods(product_id,product_count);
out.println("成功从购物车中取出商品!");
}
catch(Exception e){
out.print(e);
out.print("输入的数字不正确!");
}
%>
我要<a href="buyGoods.jsp">继续购物</a>
我要<a href="buyGoods2.jsp">查看购物车</a>
</body>
</html>
deletGoods.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<jsp:useBean id="buyCar" class="classes.BuyCar" scope="session"></jsp:useBean>
<html>
<body>
<%
String product_id=request.getParameter("product_id");
try{
if(buyCar.deleteGoods(product_id)){
out.println("成功删除购物车中的商品!");
}
else{
out.print("删除失败");
}
}
catch(Exception e){
out.print("参数不正确");
}
%>
我要<a href="buyGoods.jsp">继续购物</a>
我要<a href="buyGoods2.jsp">查看购物车</a>
</body>
</html>