您好,欢迎访问三七文档
Jsp购物车实例打开Tomcat,启动服务,在浏览器地址栏输入为自己建立的文件夹,位置为C:\Tomcat7.0\webapps\dd程序结果截图如下:购买商品操作:移除商品操作:清空购物车操作:Jsp源程序C:\Tomcat7.0\webapps\dd文件中的文件Index.jsp源程序%@pagecontentType=text/html;charset=gb2312%jsp:forwardpage=/index/Shopcar.jsp源程序%@pagecontentType=text/html;charset=gb2312%%@pageimport=java.util.ArrayList%%@pageimport=com.yxq.valuebean.GoodsSingle%%//获取存储在session中用来存储用户已购买商品的buylist集合对象ArrayListbuylist=(ArrayList)session.getAttribute(buylist);floattotal=0;//用来存储应付金额%tableborder=1width=450rules=nonecellspacing=0cellpadding=0trheight=50tdcolspan=5align=center购买的商品如下/td/trtralign=centerheight=30bgcolor=lightgreytdwidth=25%名称/tdtd价格(元/斤)/tdtd数量/tdtd总价(元)/tdtd移除(-1/次)/td/tr%if(buylist==null||buylist.size()==0){%trheight=100tdcolspan=5align=center您的购物车为空!/td/tr%}else{for(inti=0;ibuylist.size();i++){GoodsSinglesingle=(GoodsSingle)buylist.get(i);Stringname=single.getName();//获取商品名称floatprice=single.getPrice();//获取商品价格intnum=single.getNum();//获取购买数量//计算当前商品总价,并进行四舍五入floatmoney=((int)((price*num+0.05f)*10))/10f;total+=money;//计算应付金额%tralign=centerheight=50td%=name%/tdtd%=price%/tdtd%=num%/tdtd%=money%/tdtdahref=doCar?action=remove&name=%=single.getName()%移除/a/td/tr%}}%trheight=50align=centertdcolspan=5应付金额:%=total%/td/trtrheight=50align=centertdcolspan=2ahref=show.jsp继续购物/a/tdtdcolspan=3ahref=doCar?action=clear清空购物车/a/td/tr/tableShow.jsp源程序为:%@pagecontentType=text/html;charset=gb2312%%@pageimport=java.util.ArrayList%%@pageimport=com.yxq.valuebean.GoodsSingle%%ArrayListgoodslist=(ArrayList)session.getAttribute(goodslist);%tableborder=1width=450rules=nonecellspacing=0cellpadding=0trheight=50tdcolspan=3align=center提供商品如下/td/trtralign=centerheight=30bgcolor=lightgreytd名称/tdtd价格(元/斤)/tdtd购买/td/tr%if(goodslist==null||goodslist.size()==0){%trheight=100tdcolspan=3align=center没有商品可显示!/td/tr%}else{for(inti=0;igoodslist.size();i++){GoodsSinglesingle=(GoodsSingle)goodslist.get(i);%trheight=50align=centertd%=single.getName()%/tdtd%=single.getPrice()%/tdtdahref=doCar?action=buy&id=%=i%购买/a/td/tr%}}%trheight=50tdalign=centercolspan=3ahref=shopcar.jsp查看购物车/a/td/tr/tableWEB-INF文件中的文件为Web.xml内容:?xmlversion=1.0encoding=UTF-8?web-app!--配置IndexServlet--servletservlet-nameindexServlet/servlet-nameservlet-classcom.yxq.servlet.IndexServlet/servlet-class/servletservlet-mappingservlet-nameindexServlet/servlet-nameurl-pattern/index/url-pattern/servlet-mapping!--配置BuyServlet--servletservlet-namebuyServlet/servlet-nameservlet-classcom.yxq.servlet.BuyServlet/servlet-class/servletservlet-mappingservlet-namebuyServlet/servlet-nameurl-pattern/doCar/url-pattern/servlet-mapping/web-appClasses内容C:\Tomcat7.0\webapps\dd\WEB-INF\classes\com\yxqSrc内容C:\Tomcat7.0\webapps\dd\src\com\yxq编译后放入C:\Tomcat7.0\webapps\dd\WEB-INF\classes\中C:\Tomcat7.0\webapps\dd\src\com\yxq\servlet内容为BuyServlet.java源程序packagecom.yxq.servlet;importjava.io.IOException;importjava.util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.yxq.toolbean.MyTools;importcom.yxq.toolbean.ShopCar;importcom.yxq.valuebean.GoodsSingle;publicclassBuyServletextendsHttpServlet{protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringaction=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(request,response);//调用clear()方法实现购物车的清空}//实现购买商品的方法protectedvoidbuy(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{HttpSessionsession=request.getSession();StringstrId=request.getParameter(id);//获取触发“购买”请求时传递的id参数,该参数存储的是商品在goodslist对象中存储的位置intid=MyTools.strToint(strId);ArrayListgoodslist=(ArrayList)session.getAttribute(goodslist);GoodsSinglesingle=(GoodsSingle)goodslist.get(id);ArrayListbuylist=(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,buylist);response.sendRedirect(show.jsp);//将请求重定向到show.jsp页面}//实现移除商品的方法protectedvoidremove(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{HttpSessionsession=request.getSession();ArrayListbuylist=(ArrayList)s
本文标题:jsp购物车实例
链接地址:https://www.777doc.com/doc-4477943 .html