


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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(Str
3、ing 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 toChin
4、ese(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.too
5、lbean;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 sin
6、gle)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();i+)/遍历 buylist 集合对象, 判断该集合中是否已经存在
7、当前 要添加的商品GoodsSingle temp=(GoodsSingle)buylist.get(i); / 获取 buylist 集合中当前元素if(temp.getName().equals(single.getName()/判断从 buylist 集合中获取的当前商品的名称是否与 要添加的商品的名称相同/如果相同, 说明已经购买了该商品, 只需要将商品的购买数量加 1temp.setNum(temp.getNum()+1);/将商品购买数量加 1break;/结束 for 循环if(i>=buylist.size()/说明 buylist 中不存在要添加的商品 GoodsSin
8、gle 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<buylist.size();i+)/ 遍 历 buylist 集合,查找指定名称的商品GoodsSingle temp=(Good
9、sSingle)buylist.get(i);/获取集合中当前位置的商品 if(temp.getName().equals(name) / 如果商品的名称为 name 参数指定的名称if(temp.getNum()>1) / 如 果商品的购买数量大于 1temp.setNum(temp.getNum()-1);/则将购买数量减 1break;/结束 for 循环else if(temp.getNum()=1) / 如果商品的购买数量为 1buylist.remove(i); / 从 buylist 集合对象中移除该商品4、创建实例首页面index.jsp,初始化商品信息<% pag
10、e contentType="text/html;charset=gb2312"%> <jsp:forward page="/index"/>5、创 建 处 理 用 户 访 问 首 页 面 请 求 的 Servlet-IndexServletpackage com.yxq.servlet;import java.io.IOException;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.http.HttpServ
11、let;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(HttpServletR
12、equest 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("goodsl
13、ist",goodslist); response.sendRedirect("show.jsp");static/静态代码块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(namesi); single.setPrice(pricesi);single.se
14、tNum(1);goodslist.add(single);6、show.jsp显示商品信息<% page contentType="text/html;charset=gb2312"%><% page import="java.util.ArrayList" %><% page import="com.yxq.valuebean.GoodsSingle" %><% ArrayList goodslist=(ArrayList)session.getAttribute("goodsl
15、ist"); %> <table border="1" width="450" rules="none" cellspacing="0" cellpadding="0"><tr height="50"><td colspan="3" align="center"> 提供 商品如下 </td></tr><tr align="center"
16、 height="30" bgcolor="lightgrey"><td> 名称 </td><td>价格(元/斤)v/td><td> 购买 v/td></tr><% if(goodslist=null|goodslist.size()=0) %>vtr height="100"xtd colspan="3" align="center"> 没有 商品可显示! </td>v/tr>&l
17、t;%elsefor(int i=0;i<goodslistsize();i+) GoodsSinglesingle=(GoodsSingle)goodslist.get(i);%><tr height="50" align="center"><tdx%=single getName()%x/td><tdx%=single getPrice()%x/td>vtdxa href=" doCar?action=buy&id=v%=i%>"> 购 买 v/a>v/td
18、>v/tr>v%><tr height ="50" ><td align ="center"colspan ="3" >< a href ="shopcar.jsp">查看购物车 </ a></ td ></ tr ></ table >7、创建处理用户购买、移除、清空购物车请求的 ServletServlet BuyServletpackage com.yxq.servlet;import java.io.IOE
19、xception;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.Sho
20、pCar;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 respo
21、nse) 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,respons
22、e);/调用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=reques
23、t.getParameter("id");/获取触发“购买”请求时传递的 id 参数,该参数存储的是商品在 goodslist对象中存储的位置int id=MyTools.strToint(strId);ArrayListgoodslist=(ArrayList)session.getAttribute("goodslist");GoodsSingle single=(GoodsSingle)goodslist.get(id);ArrayList buylist=(ArrayList)session.getAttribute("buylist&
24、quot;);/从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
25、");/将请求重定向到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(&q
26、uot;name");ShopCar myCar=new ShopCar(); myCar.setBuylist(buylist);/将buylist对象赋值给ShopCar类实例中的属性myCar.removeItem(MyTools.toChinese(name); /调用 ShopCar 类中 removeItem () 方法实现商品移除操作response.sendRedirect("shopcar.jsp");/实现清空购物车的方法protected void clear(HttpServletRequest request, HttpServletRe
27、sponse response) throws ServletException, IOException HttpSession session=request.getSession();ArrayList buylist=(ArrayList)session.getAttribute("buylist"); / 从session范围内获取存储了用户已购买商品的集合对象buylist.clear();/清空 buylist 集合对象,实现购物车清空的操作response.sendRedirect("shopcar.jsp");8、 在 web.xml
28、文件中配置 Servlet<?xml version="1.0" encoding="UTF-8"?><web-app><!- 配置 IndexServlet -><servlet><servlet-name>indexServlet</servlet-name><servlet-class>com.yxq.servlet.IndexServlet</servlet-class></servlet><servlet-mapping>&l
29、t;servlet-name>indexServlet</servlet-name><url-pattern> /index</url-pattern></servlet-mapping><!- 配置 BuyServlet -><servlet><servlet-name>buyServlet</servlet-name><servlet-class>com.yxq.servlet.BuyServlet</servlet-class></servlet>&l
30、t;servlet-mapping><servlet-name>buyServlet</servlet-name><url-pattern> /doCar</url-pattern></servlet-mapping></web-app>9、创建页面shopcar.jsp购物车<% page contentType="text/html;charset=gb2312"%><% page import="java.util.ArrayList" %><
31、;% page import="com.yxq.valuebean.GoodsSingle" %> <%/获取存储在session中用来存储用户已购买商品的buylist集合对象ArrayList buylist=(ArrayList)session.getAttribute("buylist");float total=0;/用来存储应付金额%><table border="1" width="450" rules="none" cellspacing="0&
32、quot; cellpadding="0"><tr height="50"><td colspan="5" align="center"> 购买的商品如下 </td></tr><tr align="center" height="30" bgcolor="lightgrey"><td width="25%"> 名称 </td><td>价格(元/斤)v/td><td> 数量 </td><td> 总价(元)v/td>vtd> 移除(-1/次)</td></tr><% if(buylist=null|buylist.size()=0) %><tr height="100"><td colspan="5" align="c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 微生物检验技术核心试题及答案
- 项目创新管理与创造力的关系试题及答案
- 2024年项目管理考试反馈试题及答案
- 市场营销战略规划考核试卷
- 2024年项目管理考试动态试题及答案
- 畜牧养殖废弃物处理与利用技术研究与应用案例分析报告考核试卷
- 项目团队冲突解决的有效策略试题及答案
- 气相色谱分析试剂的选择与应用考核试卷
- 2024年项目管理考试应试技巧试题及答案
- 庆阳中式门牌楼施工方案
- 光明乳业财务战略研究
- 水电站斜井工程施工方案
- 第六单元实验活动3创新实验:二氧化碳的制取与性质一体化实验说课-2024-2025学年九年级化学人教版上册
- 工地会议室使用管理制度
- 3000道两位数进位退位加减法题1
- 2024年东南亚智能联网电视(Connected TV)市场深度研究及预测报告
- 中西医结合内科学-主治复习
- 2022年版 义务教育《数学》课程标准
- 2025深圳市中考英语 语法填空 专项复习课件
- 《铁路职业道德》课件-2.1铁路职业道德的内涵及规范
- 机器学习课件周志华Chap08集成学习
评论
0/150
提交评论