版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026学年三只熊的早餐说课稿
- 2025-2026学年北师大版数学初一说课稿
- 2025-2026学年大班特别的名字说课稿
- 2025-2026学年1到2岁做糖果的说课稿
- 2025-2026学年多声部合唱说课稿
- 2026下半年下半年小学数学教资面试乘除专项题库
- 2026下半年小学英语教资面试语法试讲
- 《德信诚学习》课件
- 解决问题二年级上册解决问题专项复习
- 虽有佳肴我的课件
- 《DB65-T 8003-2023 建设工程电子文件与电子档案管理标准》
- 2026年软件正版化工作实施方案
- 健身房投资入股协议书样本
- 钢板仓工程专项施工方案
- GB/T 45939-2025光伏组件封装用共挤胶膜
- NBT 11127-2023 在用钢丝绳芯输送带报废检测技术规范
- 母婴同室院感管理课件
- 农村初中生大五人格与生命意义感的内在关联探究
- 基层治保会培训课件
- 2025至2030年中国杭州房地产行业市场竞争现状及未来趋势研判报告
- TD/T 1024-2010县级土地利用总体规划编制规程
评论
0/150
提交评论