《jsp购物车实例(共13页).doc》由会员分享,可在线阅读,更多相关《jsp购物车实例(共13页).doc(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上Jsp购物车实例打开Tomcat,启动服务,在浏览器地址栏输入为自己建立的文件夹,位置为C:Tomcat 7.0webappsdd 程序结果截图如下:购买商品操作:移除商品操作:清空购物车操作:Jsp源程序C:Tomcat 7.0webappsdd 文件中的文件Index.jsp源程序Shopcar.jsp源程序购买的商品如下名称价格(元/斤)数量总价(元)移除(-1/次)您的购物车为空!% elsefor(int i=0;ia href=doCar?action=remove&name=移除应付金额:继续购物清空购物车Show.jsp源程序为:提供商品如下名称价格(
2、元/斤)购买没有商品可显示!% elsefor(int i=0;ia href=doCar?action=buy&id=购买查看购物车WEB-INF文件中的文件为Web.xml内容:indexServletcom.yxq.servlet.IndexServletindexServlet/index buyServletcom.yxq.servlet.BuyServletbuyServlet/doCarClasses内容C:Tomcat 7.0webappsddWEB-INFclassescomyxqSrc内容C:Tomcat 7.0webappsddsrccomyxq编译后放入C:Tomcat
3、 7.0webappsddWEB-INFclasses中C:Tomcat 7.0webappsddsrccomyxqservlet内容为BuyServlet.java源程序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.se
4、rvlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import 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
5、ServletException, IOException doPost(request,response);protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException String action=request.getParameter(action);/获取action参数值if(action=null)action=;if(action.equals(buy)/触发了“购买”请求buy(request,respons
6、e);/调用buy()方法实现商品的购买if(action.equals(remove)/触发了“移除”请求remove(request,response);/调用remove()方法实现商品的移除if(action.equals(clear)/触发了“清空购物车”请求clear(request,response);/调用clear()方法实现购物车的清空/实现购买商品的方法protected void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOExcepti
7、on HttpSession session=request.getSession();String strId=request.getParameter(id);/获取触发“购买”请求时传递的id参数,该参数存储的是商品在goodslist对象中存储的位置int id=MyTools.strToint(strId);ArrayList goodslist=(ArrayList)session.getAttribute(goodslist);GoodsSingle single=(GoodsSingle)goodslist.get(id);ArrayList buylist=(ArrayLis
8、t)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,buylist);response.sendRedirect(sho
9、w.jsp);/将请求重定向到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(name);ShopCar m
10、yCar=new ShopCar();myCar.setBuylist(buylist);/将buylist对象赋值给ShopCar类实例中的属性myCar.removeItem(MyTools.toChinese(name);/调用ShopCar类中removeItem ()方法实现商品移除操作response.sendRedirect(shopcar.jsp);/实现清空购物车的方法protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, I
11、OException HttpSession session=request.getSession();ArrayList buylist=(ArrayList)session.getAttribute(buylist);/从session范围内获取存储了用户已购买商品的集合对象buylist.clear();/清空buylist集合对象,实现购物车清空的操作response.sendRedirect(shopcar.jsp);IndexServlet.java源程序package com.yxq.servlet;import java.io.IOException;import java.u
12、til.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.valuebean.GoodsSingle;public class IndexServlet extends HttpServle
13、t private static ArrayList goodslist=new ArrayList();protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request,response);protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, I
14、OException HttpSession session=request.getSession();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);si
15、ngle.setNum(1);goodslist.add(single);C:Tomcat 7.0webappsddsrccomyxqtoolbean内容MyTools.java源程序:package com.yxq.toolbean;import java.io.UnsupportedEncodingException;public class MyTools public static int strToint(String str)/将String型数据转换为int型数据的方法if(str=null|str.equals()str=0;int i=0;tryi=Integer.parse
16、Int(str);catch(NumberFormatException e)i=0;e.printStackTrace();return i;public static String toChinese(String str)/进行转码操作的方法if(str=null)str=;try str=new String(str.getBytes(ISO-8859-1),gb2312); catch (UnsupportedEncodingException e) str=;e.printStackTrace();return str;ShopCar.java源程序package com.yxq.
17、toolbean;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(GoodsSingl
18、e single)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()/说明buylist中不存在要添加的商品GoodsSingle temp=new
19、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;i1)/如果商品的购买数量大于1temp.setNum(temp.getNum()-1);/则将购买数量减1break; /结束for循环else if(temp.get
20、Num()=1)/如果商品的购买数量为1buylist.remove(i); /从buylist集合对象中移除该商品C:Tomcat 7.0webappsddsrccomyxqvaluebean内容GoodsSingle.java源程序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) this.name = name;public int getNum() return num;public void setNum(int num) this.num = num;public float getPrice() return price;public void setPrice(float price) this.price = price;专心-专注-专业