jsp实验-应用Servlet实现购物车讲解_第1页
jsp实验-应用Servlet实现购物车讲解_第2页
jsp实验-应用Servlet实现购物车讲解_第3页
jsp实验-应用Servlet实现购物车讲解_第4页
jsp实验-应用Servlet实现购物车讲解_第5页
免费预览已结束,剩余14页可下载查看

下载本文档

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

文档简介

1、应用Servlet实现购物车具体实现过程1、 创建封装商品信息的值JavaBeanGoodsSingle 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 num;

2、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 st

3、r)/将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 s

4、tr)/进行转码操作的方法if(str=null)str=""try str=new String(str.getBytes("ISO-8859-1"),"gb2312"); catch (UnsupportedEncodingException e) str=""e.printStackTrace();return str;3、 创建购物车JavaBeanShopCar 实现添加、删除,购物车制作package com.yxq.toolbean;package com.yxq.toolbean;import ja

5、va.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!=nul

6、l)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集合对象,判断该集合中是否已经存在当前 要添加的商品GoodsSingle temp=(Goods

7、Single)buylist.get(i);获取buylist集合中当前元素if(temp.getName().equals(single.getName()/判断从buylist 集合中获取的当前商品的名称是否与要添加的商品的名称相同/如果相同,说明已经购买了该商品,只需要将商品的购买数量加1temp.setNum(temp.getNum()+1);/将商品购买数量加1break;/结束 for 循环if(i>=buylist.size()/说明buylist中不存在要添加的商品GoodsSingle temp=new GoodsSingle();temp.setName(single

8、.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=(GoodsSingle)buylist.get(i);/获取集合中当前位置的商品if(temp.getName

9、().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,初始化商品信息<% page contentType="text/html;charset=gb2312"%><jsp:fo

10、rward page="/index"/>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

11、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

12、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;i<4;i+)GoodsSingle single=new GoodsSingle();single.setName(na

13、mesi);single.setPrice(pricesi);single.setNum(1);goodslist.add(single);6、 show.jsp显示商品信息<% page contentType="text/html;charset=gb2312"%><% page import="java.util.ArrayList" %><% page import="com.yxq.valuebean.GoodsSingle" %><% ArrayListgoodslist=(Arr

14、ayList)session.getAttribute("goodslist"); %><table border="1" width="450" rules="none"cellspacing="0" cellpadding="0"> <tr height=,50,><td colspan="3" align="center"> 提供商品如下</td></tr><

15、tr align=,center, height=,30, bgcolor=,lightgrey,>< td> 名称 </td>< td>价格(元/斤)</td>< td>购买 </td></tr><% if(goodslist=nu1111goodslist.size()=0) %><tr height="100M><td colspan="3" align="center,>商品可显示! </td></tr&

16、gt;<%elsefor(int i=0;i<goodslist.size();i+)GoodsSinglesingle=(GoodsSingle)goodslist.get(i);%><tr height="50" align="center"><td><%=single.getName()%></td><td><%=single.getPrice()%></td><td><a href=" doCar?action=buy

17、&id=<%=i%>”> 购买 </a></td></tr><%><tr height ="50" >< td align ="center" colspan ="3" >< a href ="shopcar.jsp">查看购物车 </ a>< / td >< / tr >< / table >7、 创建处理用户购买、移除、清空购物车请求的ServletSer

18、vletBuyServlet package 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;im

19、port 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 d

20、oPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException String action=request.getParameter("action"); /获取 action 参数值if(action=null)action=""请求方法实现商品的购买if(action.equals("buy")buy(request,response);if(action.equals("remove&q

21、uot;)除”请求remove(request,response);remove()方法实现商品的移除if(action.equals("clear")购物车”请求clear(request,response);/触发了“购买”/调用 buy()/触发了 “移/调用/触发了“清空/调用clear()方法实现购物车的清空/实现购买商品的方法protected void buy(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException HttpSess

22、ion session=request.getSession();String strId=request.getParameter("id");/获取触发“购买”请求时传递的 id 参数,该参数存储的是商品在goodslistX寸象中存储的位置int id=MyTools.strToint(strId);ArrayListgoodslist=(ArrayList)session.getAttribute("goodslist");GoodsSingle single=(GoodsSingle)goodslist.get(id);ArrayListbuy

23、list=(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&

24、quot;,buylist);response.sendRedirect("show.jsp");/将请求重定向至“ show.jsp页面8、 在 web.xml 文件中配置Servlet<?xml version="1.0" encoding="UTF-8"?><web-app><!- 配置 IndexServlet -><servlet><servlet-name>indexServlet</servlet-name><servlet-class>

25、com.yxq.servlet.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

26、><servlet-class>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购物车<% page contentType="t

27、ext/html;charset=gb2312"%><% page import="java.util.ArrayList" %><% page import="com.yxq.valuebean.GoodsSingle" %><%获取存储在sessio冲用来存储用户已购买商品的buylist集合对象ArrayListbuylist=(ArrayList)session.getAttribute(llbuylist11);float total=0;用来存储应付金额%>< tableborder*

28、" width="450" rules=',none,1cellspacing=llO" cellpadding-'O'><tr height=,50,xtd colspan=,5,' align-'center' 购买 的商品如下v/td>v/tr><tr align'center" height="30" bgcolor="lightgrey”><td width=,25%,> 名称 v/td>vtd>价格(元/斤)</td>vtd>数量 </td>vtd> 总价(元)v/td>vtd>移除(/次)v/td></tr><% if(buylist=null|buylistsize()=O) %><tr height=',100,xtd colspan=',5,1 align="center”> 您的 购物车为空! </td></tr><%)elsefor(int i=O;i<buylist.size();i+)GoodsSingle

温馨提示

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

评论

0/150

提交评论