用JSP的Session 机制编写的程序就可以是你拥有一个功能.docx
上传人:qw****27 上传时间:2024-09-12 格式:DOCX 页数:6 大小:23KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

用JSP的Session 机制编写的程序就可以是你拥有一个功能.docx

用JSP的Session机制编写的程序就可以是你拥有一个功能.docx

预览

在线预览结束,喜欢就下载吧,查找使用更方便

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

用JSP的Session机制编写的程序就可以是你拥有一个功能强大购物车程序,是不是很诱人呢?赶紧开始我们的程序吧JSPSession机制购物车之一构建的商品类◆写一个Goods类,并定义商品的各个属性,返回商品属性的方法,以及商品对象进行比较的方法◆Goods.HYPERLINK"http://www.189works.com/tech/devlodoc/java/"\t"_blank"javapackagecom.viita.Shop;publicclassGoodsimplementsComparable{◆初始化各成员变量privateStringId=null;//商品的编号IdprivateStringname=null;//商品的名称nameprivatefloatprice=0.00F;//商品的价格priceprivateintnumber=0;//商品的数量numberpublicGoods(StringId,Stringname,floatprice,intnumber){this.Id=Id;this.name=name;this.price=price;this.number=number;}publicStringgetId()//返回订购商品的编号Id{returnthis.Id;}publicStringgetName()//返回订购商品的名称name{returnthis.name;}publicfloatgetPrice()//返回订购商品的价格price{returnthis.price;}publicintgetNumber()//返回订购商品的数量number{returnthis.number;}publicintcompareTo(Objectm){//TODOAuto-generatedmethodstubGoodsn=(Goods)m;intcomRs=Id.compareTo(n.Id);returncomRs;}}JSPSession机制购物车之二购物车实现◆首先建立Goods(商品)对象goods,并建立建立ArrayList对象ay◆通过ArrayList对象的方法add()将商品对象添加到ArrayList对象ay中◆由于ArrayList对象是具有添加和删除成员的方法,从而实现多个商品存储管理于ArrayList对象◆将ArrayList对象ay存储于session对象当中,实现购物车功能◆shopcar.jsp<%@pagelanguage="java"import="java.sql.*,com.viita.Shop.*,java.util.*"pageEncoding="GBK"%><%◆设置编码格式request.setCharacterEncoding("GBK");◆获取参数信息Stringid=request.getParameter("id");Stringname=request.getParameter("name");intnumber=java.lang.Integer.parseInt(request.getParameter("number"));floatprice=java.lang.Float.parseFloat(request.getParameter("price"));◆建立商品对象和ArrayList对象Goodsgoods=newGoods(id,name,price,number);ArrayListay=null;◆如果session中从未写入过,则将建立的商品对象添加到ArrayList对象当中,并写入sessionif((ArrayList)session.getAttribute("car")==null){ay=newArrayList();ay.add(goods);session.setAttribute("car",ay);response.sendRedirect("order_index.jsp");}◆如果写如过,则将商品对象添加到ArrayList对象当中,并写入sessionelse{ay=(ArrayList)session.getAttribute("car");◆如果ArrayList对象为空,则直接添加到ArrayList对象当中if(ay.isEmpty()){ay.add(goods);session.setAttribute("car",ay);response.sendRedirect("order_inde