jsp实验-应用Servlet实现购物车讲解_第1页
jsp实验-应用Servlet实现购物车讲解_第2页
jsp实验-应用Servlet实现购物车讲解_第3页
jsp实验-应用Servlet实现购物车讲解_第4页
jsp实验-应用Servlet实现购物车讲解_第5页
已阅读5页,还剩15页未读, 继续免费阅读

付费下载

下载本文档

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

文档简介

1、GoodsSingle/保存商品名称/保存商品价格 /保存商品购买数量应用 Servlet 实现购物车具体实现过程1、 创建封装商品信息的值 JavaBean package 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

2、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(Stri

3、ng 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)/ 进行转码操作的

4、方法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;

5、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 中不存在任何商品GoodsSingl

6、e 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(sin

7、gle.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 集合

8、对象中移除该商品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.ser

9、vlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.yxq.valuebean.GoodsSingle;public class IndexServlet extends HttpServlet protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException HttpSession session=request.getS

10、ession(); 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、sh

11、ow.jsp显示商品信息 提供 商品如下 名称 价格(元/斤)v/td 购买 v/td 没有 商品可显示! vtr height=50 align=centerv%=single getName()%x/tdv%=single getPrice()%x/tdvtdxa href= doCar?action=buy&id=v%=i%购买 v/av/td v/trv%查看购物车 7、创建处理用户购买、移除、清空购物车请求的ServletServletBuyServlet package com.yxq. servlet;import java.io.IOException;import java.

12、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.v

13、aluebean.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 ServletE

14、xception, 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(re

15、quest,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=MyToo

16、ls.strToint(strId);ArrayListgoodslist=(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.setBuy

17、list(buylist);/将buylist对象赋值给ShopCar类实例中的属性myCar.addItem(single);/调用ShopCar 类中 addItem() 方法实现商品添加操作/将请求session.setAttribute(buylist,buylist); response.sendRedirect(show.jsp);重定向到show.jsp页面8、 在 web.xml 文件中配置 Servlet indexServletcom.yxq.servlet.IndexServletindexServlet /indexbuyServletcom.yxq.servlet.BuyServletbuyServlet /doCar9、创建页面shopcar.jsp购物车 购买 的商品如下 名称 价格(元/斤)v/td数量 v/td 总价(元)vtd 移除(-1/次)v/trv% if(buylist=null|buylist.size()=0) %vtr height=100vtd colspan=5 align=center 您的 购物车为空! v/tdv/trv%elsefor(int i=0;ivbuylist.size();i+)GoodsSingle single

温馨提示

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

评论

0/150

提交评论