应用Servlet实现购物车
具体实现过程
1、 创建封装商品信息的值JavaBean---------GoodsSingle
package com.yxq.valuebean;
public class GoodsSingle {
private String name; private float price; private int num;
//保存商品名称 //保存商品价格 //保存商品购买数量
public String getName() { }
public void setName(String name) { }
public int getNum() { }
public void setNum(int num) {
this.num = num; return num; this.name = name; return name;
. 专业.专注 .
. . .. . .
}
}
public float getPrice() { }
public void setPrice(float price) {
this.price = price; return price;
}
2、 创建工具JavaBean-------- MyTools 实现字符型数据转换为整型及乱码处理
package com.yxq.toolbean;
import java.io.UnsupportedEncodingException;
public class MyTools {
public static int strToint(String str){
//将String
型数据转换为int型数据的方法
if(str==null||str.equals(\"\"))
str=\"0\";
int i=0; try{
. 专业.专注 .
. . .. . .
i=Integer.parseInt(str); //把str 转换
成 int 类型的变量
}catch(NumberFormatException e){ //
try-catch就是监视try中的语句,如果抛出catch中声明的异常类型 i=0;
e.printStackTrace(); //把
Exception的详细信息打印出来 }
public static String toChinese(String str){
//进行转码
} return i;
操作的方法
if(str==null)
str=\"\";
try {
str=new
String(str.getBytes(\"ISO-8859-1\"),\"gb2312\");
} catch (UnsupportedEncodingException e) { }
str=\"\";
e.printStackTrace();
. 专业.专注 .
. . .. . .
}
}
return str;
3、 创建购物车JavaBean------ ShopCar实现添加、删
除,购物车制作
package com.yxq.toolbean; package com.yxq.toolbean;
import java.util.ArrayList;
import com.yxq.valuebean.GoodsSingle;
public class ShopCar {
private ArrayList buylist=new ArrayList();
//用来存储购买的商品
public void setBuylist(ArrayList buylist) { } /**
* @功能 向购物车中添加商品 this.buylist = buylist;
. 专业.专注 .
. . .. . .
* @参数 single为GoodsSingle类对象,封装了要添加的
商品信息 */
public void addItem(GoodsSingle single){
if(single!=null){
if(buylist.size()==0){
//如果buylist中不存在任何商品 } else{
GoodsSingle temp=new GoodsSingle(); temp.setName(single.getName()); temp.setPrice(single.getPrice()); temp.setNum(single.getNum()); buylist.add(temp);
//存储商品
//如果buylist中存在商品
int i=0;
for(;i 当前要添加的商品 GoodsSingle //获取buylist temp=(GoodsSingle)buylist.get(i); 集合中当前元素 . 专业.专注 . . . .. . . if(temp.getName().equals(single.getName())){ //判断从buylist集合中获取的当前商品的名称是否与要添 加的商品的名称相同 //如果相同,说明已经购买了该商品,只需 要将商品的购买数量加1 temp.setNum(temp.getNum()+1); //将商品购买数量加1 break; //结束for循环 } if(i>=buylist.size()){ } //说明buylist中不存在要添加的商品 GoodsSingle temp=new GoodsSingle(); temp.setName(single.getName()); temp.setPrice(single.getPrice()); temp.setNum(single.getNum()); buylist.add(temp); //存储商品 } } . 专业.专注 . . . .. . . } } /** * @功能 从购物车中移除指定名称的商品 * @参数 name表示商品名称 */ public void removeItem(String name){ for(int i=0;i 集合,查找指定名称的商品 GoodsSingle temp=(GoodsSingle)buylist.get(i); //获取集合中当前位置的商品 if(temp.getName().equals(name)){ //如果商品的名称为name参数指定的名称 if(temp.getNum()>1){ // 如果商品的购买数量大于1 temp.setNum(temp.getNum()-1); //则将购买数量减1 break; // 结束for循环 } else if(temp.getNum()==1){ //如果商品的购买数量为1 . 专业.专注 . . . .. . . buylist.remove(i); //从 buylist集合对象中移除该商品 } 4、 创建实例首页面index.jsp,初始化商品信息 <%@ contentType=\"text/html;charset=gb2312\"%> 5、 创建处理用户访问首页面请求的Servlet--- IndexServlet package com.yxq.servlet; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; . 专业.专注 . } } } } page . . .. . . import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.yxq.valuebean.GoodsSingle; public class IndexServlet extends HttpServlet { private static ArrayList goodslist=new ArrayList(); protected void doGet(HttpServletRequest request, response) throws HttpServletResponse ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, response) throws HttpServletResponse ServletException, IOException { } static{ //静态代码块 HttpSession session=request.getSession(); session.setAttribute(\"goodslist\response.sendRedirect(\"show.jsp\"); String[] names={\"苹果\香蕉\梨\橘子\float[] prices={2.8f,3.1f,2.5f,2.3f}; . 专业.专注 . . . .. . . } } for(int i=0;i<4;i++){ } GoodsSingle single=new GoodsSingle(); single.setName(names[i]); single.setPrice(prices[i]); single.setNum(1); goodslist.add(single); 6、 show.jsp显示商品信息 <%@ contentType=\"text/html;charset=gb2312\"%> <%@ page import=\"java.util.ArrayList\" %> <%@ import=\"com.yxq.valuebean.GoodsSingle\" %> <% ArrayList goodslist=(ArrayList)session.getAttribute(\"goodslist\"); width=\"450\" rules=\"none\" %> page page . 专业.专注 . . . .. . . cellspacing=\"0\" cellpadding=\"0\"> 提供商品如下 height=\"30\" bgcolor=\"lightgrey\"> <% if(goodslist==null||goodslist.size()==0){ %> 没有商品可显示! for(int i=0;i single=(GoodsSingle)goodslist.get(i); %> . 专业.专注 . . . .. . . 买 href=\"doCar?action=buy&id=<%=i%>\"> %> 7、 创建处理用户购买、移除、清空购物车请求的ServletServlet----- BuyServlet package com.yxq.servlet; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.yxq.toolbean.MyTools; . 专业.专注 . . . .. . . import com.yxq.toolbean.ShopCar; import com.yxq.valuebean.GoodsSingle; public class BuyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, doPost(request,response); HttpServletResponse response) throws ServletException, IOException { String action=request.getParameter(\"action\"); // 获取action参数值 if(action==null) action=\"\"; //触发了“购 if(action.equals(\"buy\")) 买”请求 buy(request,response); //调用 buy()方法实现商品的购买 . 专业.专注 . . . .. . . if(action.equals(\"remove\")) //触发了 “移除”请求 remove(request,response); //调 用remove()方法实现商品的移除 if(action.equals(\"clear\")) //触发了 “清空购物车”请求 clear(request,response); //调用 clear()方法实现购物车的清空 } //实现购买商品的方法 protected void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); String strId=request.getParameter(\"id\"); //获 取触发“购买”请求时传递的id参数,该参数存储的是商品在goodslist对象中存储的位置 int id=MyTools.strToint(strId); ArrayList goodslist=(ArrayList)session.getAttribute(\"goodslist\"); GoodsSingle . 专业.专注 . . . .. . . single=(GoodsSingle)goodslist.get(id); ArrayList // buylist=(ArrayList)session.getAttribute(\"buylist\"); 从session范围内获取存储了用户已购买商品的集合对象 if(buylist==null) ShopCar myCar=new ShopCar(); myCar.setBuylist(buylist); //将 buylist=new ArrayList(); buylist对象赋值给ShopCar类实例中的属性 myCar.addItem(single); //调用 ShopCar类中addItem()方法实现商品添加操作 session.setAttribute(\"buylist\response.sendRedirect(\"show.jsp\"); //将 请求重定向到show.jsp页面 } //实现移除商品的方法 protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { . 专业.专注 . . . .. . . HttpSession session=request.getSession(); ArrayList buylist=(ArrayList)session.getAttribute(\"buylist\"); String name=request.getParameter(\"name\"); ShopCar myCar=new ShopCar(); myCar.setBuylist(buylist); //将 buylist对象赋值给ShopCar类实例中的属性 myCar.removeItem(MyTools.toChinese(name)); //调用ShopCar类中removeItem ()方法实现商品移除 操作 } //实现清空购物车的方法 protected void clear(HttpServletRequest request, response.sendRedirect(\"shopcar.jsp\"); HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); ArrayList buylist=(ArrayList)session.getAttribute(\"buylist\"); //从session范围内获取存储了用户已购买商品的集合对 . 专业.专注 . . . .. . . 象 } 8、 在web.xml文件中配置Servlet buylist.clear(); //清空buylist集合对象,实现购物车清空的操作 response.sendRedirect(\"shopcar.jsp\"); t-class> . 专业.专注 . . . .. . . 9、 创建页面shopcar.jsp购物车 <%@ contentType=\"text/html;charset=gb2312\"%> <%@ page import=\"java.util.ArrayList\" %> <%@ import=\"com.yxq.valuebean.GoodsSingle\" %> . 专业.专注 . page page . . .. . . <% //获取存储在session中用来存储用户已购买商品的 buylist集合对象 ArrayList buylist=(ArrayList)session.getAttribute(\"buylist\"); float total=0; //用来存储应付金额 %> width=\"450\" rules=\"none\" cellspacing=\"0\" cellpadding=\"0\"> 购买的商品如下 height=\"30\" bgcolor=\"lightgrey\"> <% if(buylist==null||buylist.size()==0){ %> 您的购物车为空! . 专业.专注 . . . .. . . <% } else{ for(int i=0;i single=(GoodsSingle)buylist.get(i); String name=single.getName(); //获取商品 名称 float price=single.getPrice(); //获取商品 价格 int num=single.getNum(); //获取购买 数量 //计算当前商品总价,并进行四舍五入 float money=((int)((price*num+0.05f)*10))/10f; total+=money; //计算应付 金额 %> . 专业.专注 . . . .. . . %> 应付金额:<%=total%> . 专业.专注 . 因篇幅问题不能全部显示,请点此查看更多更全内容border=\"1\"
align=\"center\" 名称 价格(元/斤) 购买 <% } else{ <% } }<%=single.getName()%> <%=single.getPrice()%> 购 border=\"1\"
align=\"center\" 名称 价格(元/斤) 数量 总价(元) 移除(-1/次) <% } }<%=name%> <%=price%> <%=num%> <%=money%> href=\"doCar?action=remove&name=<%=single.getName() %>\">移除 继续购物 colspan=\"3\">href=\"doCar?action=clear\">清空购物车