版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、GoodsSingle/保存商品名称/保存商品价格/保存商品购买数量应用Servlet实现购物车具体实现过程1、创建封装商品信息的值JavaBeanpackagecom.yxq.valuebean;publicclassGoodsSingleprivateStringname;privatefloatprice;privateintnum;publicStringgetName()returnname;publicvoidsetName(Stringname)=name;publicintgetNum()returnnum;publicvoidsetNum(intnum)th
2、is.num=num;publicfloatgetPrice()returnprice;publicvoidsetPrice(floatprice)this.price=price;2、创建工具JavaBeanMyTools实现字符型数据转换为整型及乱码处理packagecom.yxq.toolbean;importjava.io.UnsupportedEncodingException;publicclassMyToolspublicstaticintstrToint(Stringstr)/将String型数据转换为int型数据的方法if(str=null|str.equals("
3、")str="0"inti=0;tryi=Integer.parseInt(str);/把str转换成int类型的变量catch(NumberFormatExceptione)/try-catch就是监视try中的语句,如果抛出catch中声明的异常类型i=0;e.printStackTrace();/把Exception的详细信息打印出来returni;publicstaticStringtoChinese(Stringstr)/进行转码操作的方法if(str=null)str=""trystr=newString(str.getBytes(
4、"ISO-8859-1"),"gb2312");catch(UnsupportedEncodingExceptione)str=""e.printStackTrace();returnstr;3、创建购物车JavaBeanShopCar实现添加、删除,购物车制作packagecom.yxq.toolbean;packagecom.yxq.toolbean;importjava.util.ArrayList;importcom.yxq.valuebean.GoodsSingle;publicclassShopCarprivateArra
5、yListbuylist=newArrayList();/用来存储购买的商品publicvoidsetBuylist(ArrayListbuylist)this.buylist=buylist;/*功能向购物车中添加商品*参数single为GoodsSingle类对象,圭寸装了要添加的商品信息*/publicvoidaddItem(GoodsSinglesingle)if(single!=null)if(buylist.size()=0)/如果buylist中不存在任何商品GoodsSingletemp=newGoodsSingle();temp.setName(single.getName(
6、);temp.setPrice(single.getPrice();temp.setNum(single.getNum();buylist.add(temp);/存储商品else/如果buylist中存在商品inti=0;for(;i<buylist.size();i+)/遍历buylist集合对象,判断该集合中是否已经存在当前要添加的商品GoodsSingletemp=(GoodsSingle)buylist.get(i);/获取buylist集合中当前元素if(temp.getName().equals(single.getName()/判断从buylist集合中获取的当前商品的名称
7、是否与要添加的商品的名称相同/如果相同,说明已经购买了该商品,只需要将商品的购买数量加1temp.setNum(temp.getNum()+1);/将商品购买数量加1break;/结束for循环if(i>=buylist.size()/说明buylist中不存在要添加的商品GoodsSingletemp=newGoodsSingle();temp.setName(single.getName();temp.setPrice(single.getPrice();temp.setNum(single.getNum();buylist.add(temp);/存储商品/*功能从购物车中移除指定名
8、称的商品*参数name表示商品名称*/publicvoidremoveItem(Stringname)for(inti=0;i<buylist.size();i+)/遍历buylist集合,查找指定名称的商品GoodsSingletemp=(GoodsSingle)buylist.get(i);/获取集合中当前位置的商品if(temp.getName().equals(name)/如果商品的名称为name参数指定的名称if(temp.getNum()>1)/如果商品的购买数量大于1temp.setNum(temp.getNum()-1);/则将购买数量减1break;/结束for循
9、环elseif(temp.getNum()=1)/如果商品的购买数量为1buylist.remove(i);/从buylist集合对象中移除该商品4、创建实例首页面index.jsp,初始化商品信息<%pagecontentType="text/html;charset=gb2312"%><jsp:forwardpage="/index"/>5、创建处理用户访问首页面请求的Servlet-IndexServletpackagecom.yxq.servlet;importjava.io.IOException;importjava.
10、util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.yxq.valuebean.GoodsSingle;publicclassIndexServletextendsHttpServletprotecte
11、dvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOExceptionHttpSessionsession=request.getSession();session.setAttribute("goodslist",goodslist);response.sendRedirect("show.jsp");static/静态代码块Stringnames="苹果","香蕉","梨&
12、quot;,"橘子"floatprices=2.8f,3.1f,2.5f,2.3f;for(inti=0;i<4;i+)GoodsSinglesingle=newGoodsSingle();single.setName(namesi);single.setPrice(pricesi);single.setNum(1);goodslist.add(single);6、show.jsp显示商品信息<%pagecontentType="text/html;charset=gb2312"%><%pageimport="java.
13、util.ArrayList"%><%pageimport="com.yxq.valuebean.GoodsSingle"%><%ArrayListgoodslist=(ArrayList)session.getAttribute("goodslist");%><tableborder="1"width="450"rules="none"cellspacing="0"cellpadding="0"><
14、;trheight="50"><tdcolspan="3"align="center">提供商品如下</td></tr><tralign="center"height="30"bgcolor="lightgrey"><td>名称</td><td>价格(元/斤)v/td><td>购买v/td></tr><%if(goodslist=null|goo
15、dslist.size()=0)%><trheight="100"><tdcolspan="3"align="center">没有商品可显示!</td></tr><%elsefor(inti=0;ivgoodslist.size();i+)GoodsSinglesingle=(GoodsSingle)goodslist.get(i);%>vtrheight="50"align="center"><td>v%=si
16、nglegetName()%x/td><td>v%=singlegetPrice()%x/td>vtdxahref="doCar?action=buy&id=v%=i%>">购买v/a>v/td>v/tr>v%><trheight="50"><tdalign="center"colspan="3"><ahref="shopcar.jsp">查看购物车</a></td>&
17、lt;/tr></table>7、创建处理用户购买、移除、清空购物车请求的ServletServletBuyServletpackagecom.yxq.servlet;importjava.io.IOException;importjava.util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletR
18、esponse;importjavax.servlet.http.HttpSession;importcom.yxq.toolbean.MyTools;importcom.yxq.toolbean.ShopCar;importcom.yxq.valuebean.GoodsSingle;publicclassBuyServletextendsHttpServletprotectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOExceptiondoPost(requ
19、est,response);protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOExceptionStringaction=request.getParameter("action");/获取action参数值if(action=null)action=""if(action.equals("buy")/触发了“购买”请求buy(request,response);/调用buy()方法实现商
20、品的购买if(action.equals("remove")/触发了“移除”请求remove(request,response);/调用remove()方法实现商品的移除if(action.equals("clear")/触发了“清空购物车”请求clear(request,response);/调用clear()方法实现购物车的清空/实现购买商品的方法protectedvoidbuy(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOExcepti
21、onHttpSessionsession=request.getSession();StringstrId=request.getParameter("id");/获取触发“购买”请求时传递的id参数,该参数存储的是商品在goodslist对象中存储的位置intid=MyTools.strToint(strId);ArrayListgoodslist=(ArrayList)session.getAttribute("goodslist");GoodsSinglesingle=(GoodsSingle)goodslist.get(id);ArrayList
22、buylist=(ArrayList)session.getAttribute("buylist");/从session范围内获取存储了用户已购买商品的集合对象if(buylist=null)buylist=newArrayList();ShopCarmyCar=newShopCar();myCar.setBuylist(buylist);/将buylist对象赋值给ShopCar类实例中的属性myCar.addItem(single);/调用ShopCar类中addItem()方法实现商品添加操作/将请求session.setAttribute("buylist
23、",buylist);response.sendRedirect("show.jsp");重定向到show.jsp页面8、在web.xml文件中配置Servlet<?xmlversion="1.0"encoding="UTF-8"?><web-app><!-配置IndexServlet-><servlet><servlet-name>indexServlet</servlet-name><servlet-class>com.yxq.servl
24、et.IndexServlet</servlet-class></servlet><servlet-mapping><servlet-name>indexServlet</servlet-name><url-pattern>/index</url-pattern></servlet-mapping><!-配置BuyServlet-><servlet><servlet-name>buyServlet</servlet-name><servlet-c
25、lass>com.yxq.servlet.BuyServlet</servlet-class></servlet><servlet-mapping><servlet-name>buyServlet</servlet-name><url-pattern>/doCar</url-pattern></servlet-mapping></web-app>9、创建页面shopcar.jsp购物车<%pagecontentType="text/html;charset=gb23
26、12"%><%pageimport="java.util.ArrayList"%><%pageimport="com.yxq.valuebean.GoodsSingle"%><%/获取存储在session中用来存储用户已购买商品的buylist集合对象ArrayListbuylist=(ArrayList)session.getAttribute("buylist");floattotal=0;/用来存储应付金额%><tableborder="1"width
27、="450"rules="none"cellspacing="0"cellpadding="0"><trheight="50"><tdcolspan="5"align="center">购买的商品如下</td></tr><tralign="center"height="30"bgcolor="lightgrey"><tdwid
28、th="25%">名称</td><td>价格(元/斤)v/td><td>数量v/td><td>总价(元)</td>vtd>移除(-1/次)</td>v/tr>v%if(buylist=null|buylist.size()=0)%>vtrheight="100">vtdcolspan="5"align="center">您的购物车为空!v/td>v/tr>v%elsefor(inti=0;ivbuylist.size();i+)GoodsSinglesingle=(GoodsSing
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年伊利集团物流经理面试题库含答案
- 《DLT 5161.5-2018电气装置安装工程质量检验及评定规程 第5部分:电缆线路施工质量检验》专题研究报告
- 《DLT 1933.4-2018塑料光纤信息传输技术实施规范 第4部分:塑料光缆》专题研究报告
- 2026年药剂师面试题及答案集
- 2026年公务员考试申论写作高分技巧含答案
- 2026年企业内审面试常见问题解析
- 2026年内容编辑面试题及文案写作含答案
- 2026年电力行业工程师面试题库
- 2026年物流科技公司项目经理考试题目及答案
- 2026年互联网行业软件测试分析师岗位技能与面试准备
- 中职中医教师面试题库及答案
- 2026年关于汽车销售工作计划书
- 2025年汕头市金平区教师招聘笔试参考试题及答案解析
- T∕ACEF 235-2025 企业环境社会治理(ESG)评价机构要求
- 拆迁工程安全监测方案
- 视频会议系统施工质量控制方案
- 质量环境及职业健康安全三体系风险和机遇识别评价分析及控制措施表(包含气候变化)
- 2025至2030防雷行业项目调研及市场前景预测评估报告
- 2025年护理三基考试卷(含答案)
- 除夕烟火秀活动方案
- 地理中国的工业+课件-2025-2026学年初中地理湘教版八年级上册
评论
0/150
提交评论