应用Servlet实现购物车.doc_第1页
应用Servlet实现购物车.doc_第2页
应用Servlet实现购物车.doc_第3页
应用Servlet实现购物车.doc_第4页
应用Servlet实现购物车.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

精品文档应用Servlet实现购物车具体实现过程1、 创建封装商品信息的值JavaBean-GoodsSinglepackage com.yxq.valuebean;public class GoodsSingle private String name;/保存商品名称private float price; /保存商品价格private int num;/保存商品购买数量public String getName() return name;public void setName(String name) = name;public int getNum() return num;public void setNum(int num) this.num = num;public float getPrice() return price;public void setPrice(float price) this.price = 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;tryi=Integer.parseInt(str); /把str 转换成 int 类型的变量catch(NumberFormatException e) / try-catch就是监视try中的语句,如果抛出catch中声明的异常类型i=0;e.printStackTrace(); /把Exception的详细信息打印出来return i;public static String toChinese(String str)/进行转码操作的方法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中不存在任何商品GoodsSingle temp=new GoodsSingle();temp.setName(single.getName();temp.setPrice(single.getPrice();temp.setNum(single.getNum();buylist.add(temp);/存储商品else/如果buylist中存在商品int i=0;for(;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;i1)/如果商品的购买数量大于1temp.setNum(temp.getNum()-1);/则将购买数量减1break; /结束for循环else if(temp.getNum()=1)/如果商品的购买数量为1buylist.remove(i); /从buylist集合对象中移除该商品4、 创建实例首页面index.jsp,初始化商品信息5、 创建处理用户访问首页面请求的Servlet- IndexServletpackage 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.valuebean.GoodsSingle;public class IndexServlet extends HttpServlet private static ArrayList goodslist=new ArrayList();protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request,response);protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException HttpSession session=request.getSession();session.setAttribute(goodslist,goodslist);response.sendRedirect(show.jsp);static/静态代码块String names=苹果,香蕉,梨,橘子;float prices=2.8f,3.1f,2.5f,2.3f;for(int i=0;i4;i+)GoodsSingle single=new GoodsSingle();single.setName(namesi);single.setPrice(pricesi);single.setNum(1);goodslist.add(single);6、 show.jsp显示商品信息提供商品如下名称价格(元/斤)购买没有商品可显示!% elsefor(int i=0;ia href=doCar?action=buy&id=购买7、 创建处理用户购买、移除、清空购物车请求的Servlet Servlet- BuyServletpackage 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 doPost(request,response);protected void doPost(HttpServletRequest request, 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)buylist=new ArrayList();ShopCar myCar=new ShopCar();myCar.setBuylist(buylist); /将buylist对象赋值给ShopCar类实例中的属性myCar.addItem(single);/调用ShopCar类中addItem()方法实现商品添加操作session.setAttribute(buylist,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 ()方法实现商品移除操作response.sendRedirect(shopcar.jsp);/实现清空购物车的方法protected void clear(HttpSe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论