一、构建的商品类

//写一个Goods类,并定义商品的各个属性,返回商品属性的方法,以及商品对象进行比较的方法
//Goods.java

package com.viita.Shop;

public class Goods implements Comparable {

//    初始化各成员变量

private String Id = null;//商品的编号Id

private String name = null;//商品的名称name

private float price = 0.00F;//商品的价格price

private int number = 0;//商品的数量number

public Goods(String Id, String name, float price, int number) {
        this.Id = Id;
        this.name = name;
        this.price = price;
        this.number = number;

}

public String getId() //返回订购商品的编号Id

{
        return this.Id;
    }

public String getName() //返回订购商品的名称name

{
        return this.name;
    }

public float getPrice() //返回订购商品的价格price

{
        return this.price;
    }

public int getNumber() //返回订购商品的数量number

{
        return this.number;
    }

public int compareTo(Object m) {
        // TODO Auto-generated method stub

Goods n = (Goods) m;
        int comRs = Id.compareTo(n.Id);
        return comRs;

}

}

二、购物车

//首先建立Goods(商品)对象goods,并建立建立ArrayList对象ay
//通过ArrayList对象的方法add()将商品对象添加到ArrayList对象ay中
//由于ArrayList对象是具有添加和删除成员的方法,从而实现多个商品存储管理于ArrayList对象
//将ArrayList对象ay存储于session对象当中,实现购物车功能
//shopcar.jsp

<%@ page language="java" import=" java.sql.*,com.viita.Shop.*,java.util.*" pageEncoding="GBK"%>
<%
//设置编码格式

request.setCharacterEncoding("GBK");
//获取参数信息

String id = request.getParameter("id");
String name = request.getParameter("name");
int number = java.lang.Integer.parseInt(request.getParameter("number"));
float price= java.lang.Float.parseFloat(request.getParameter("price"));

//建立商品对象和ArrayList对象

Goods goods = new Goods(id,name,price,number);
ArrayList ay = null;
//如果session中从未写入过,则将建立的商品对象添加到ArrayList对象当中,并写入 session

if((ArrayList)session.getAttribute("car")==null)
{
    ay = new ArrayList();
    ay.add(goods);
    session.setAttribute("car",ay);
    response.sendRedirect("order_index.jsp");
}
//如果写如过,则将商品对象添加到ArrayList对象当中,并写入 session

else
{
ay=(ArrayList)session.getAttribute("car");
    //如果ArrayList 对象为空,则直接添加到ArrayList对象当中

if(ay.isEmpty())
{
        ay.add(goods);
        session.setAttribute("car",ay);
        response.sendRedirect("order_index.jsp");
    }
    //如果ArrayList 对象不为空,则判断购入商品是否已经存在于车中

else
{
        Iterator it = ay.iterator();
        for(int i = 0;i<ay.size();i++) //下面还有另一种遍历方法

{
            Goods shop = (Goods)it.next();
//如果购入商品已经存在,则打印输入提示信息

if(shop.compareTo(goods)==0)
{
            out.println(" ");
            }
//如果购入商品不存在,则直接将商品添加到ArrayList对象当中,并写入 session

else
            {
            ay.add(goods);
            session.setAttribute("car",ay);
            response.sendRedirect("order_index.jsp");
            }
        }
    }
}
%>

三、删除商品

//对购物车中的商品进行删除操作
//removeGoods.jsp

<%@ page language="java" import="java.sql.*,com.viita.Shop.*,java.util.*" pageEncoding="GBK"%>
<%
//设置编码格式

request.setCharacterEncoding("gb2313");
//获取参数信息

String id = request.getParameter("id");
String name = request.getParameter("name");
float price = java.lang.Float.parseFloat(request.getParameter("price"));
int number = java.lang.Integer.parseInt(request.getParameter("number"));
//创建符合条件参数要删除的商品对象

Goods goods = new Goods(id,name,price,number);
//获取session 中存储的ArrayList对象

ArrayList ay = (ArrayList)session.getAttribute("car");
Iterator it = ay.iterator();
//遍历ArrayList对象,并将ArrayList对象中的元素和创建的符合参数条件要删除的商品进行比较

for(int i = ay.size();it.hasNext();i--)
{
    Goods shop = (Goods)it.next();
    
//查询是否有ArrayList对象中的元素与要删除的商品相同

if(shop.compareTo(goods)==0)
{
        int index = ay.indexOf(shop);
        
//如果ArrayList对象已经为空,则跳转

if(ay.isEmpty())
{
            response.sendRedirect("order_index.jsp");
        }
        
//如果ArrayList对象不为空,则从其中移去要与要删除的商品条件相符的元素,并重新写session

else
{
            ay.remove(index);
            session.setAttribute("car",ay);
            response.sendRedirect("order_index.jsp");
        }
    }
    else
{
        out.print("程序异常");
    }
}
%>

[转]用 Jsp 的 Session 机制编写的购物车程序的更多相关文章

  1. 【Tomcat】JSP使用Session、Cookie实现购物车

    购物界面shop.jsp 初始页面 添加商品后,在session中设置属性,重定向回到shop.jsp,然后根据session的内容显示结果 Cookie设置setMaxAge可以延长session的 ...

  2. Session机制详解

    转自:http://justsee.iteye.com/blog/1570652 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能 ...

  3. cookie,Session机制的本质,跨应用程序的session共享

    目录:一.术语session二.HTTP协议与状态保持三.理解cookie机制四.理解session机制五.理解javax.servlet.http.HttpSession六.HttpSession常 ...

  4. JavaWeb---总结(十九)Session机制

    一.术语session session,中文经常翻译为会话,其本来的含义是指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个session.有时候我们可 ...

  5. session 机制和 httpsession 详解 (转载)

    https://www.cnblogs.com/bjanzhuo/archive/2013/02/27/3575884.html 一.术语session 在我的经验里,session这个词被滥用的程度 ...

  6. 理解Cookie和Session机制(转)

    目录[-] Cookie机制 什么是Cookie 记录用户访问次数 Cookie的不可跨域名性 Unicode编码:保存中文 BASE64编码:保存二进制图片 设置Cookie的所有属性 Cookie ...

  7. session机制详解以及session的相关应用

    session是web开发里一个重要的概念,在大多数web应用里session都是被当做现成的东西,拿来就直接用,但是一些复杂的web应用里能拿来用的session已经满足不了实际的需求,当碰到这样的 ...

  8. 理解Cookie和Session机制

    转载: 理解Cookie和Session机制 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录 ...

  9. cookie机制和session机制的原理和区别[转]

    一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时我们也看到,由于在服务器端保持状态的 ...

随机推荐

  1. document.write 简介

    document.write 的执行分两种情况: 第一种:dom加载已完成 1. 执行 document.open() (即会清空document) 2. 执行 document.write() 3. ...

  2. python并发编程之asyncio协程(三)

    协程实现了在单线程下的并发,每个协程共享线程的几乎所有的资源,除了协程自己私有的上下文栈:协程的切换属于程序级别的切换,对于操作系统来说是无感知的,因此切换速度更快.开销更小.效率更高,在有多IO操作 ...

  3. C++之模板编程

    当我们越来越多的使用C++的特性, 将越来越多的问题和事物抽象成对象时, 我们不难发现:很多对象都具有共性. 比如 数值可以增加.减少:字符串也可以增加减少. 它们的动作是相似的, 只是对象的类型不同 ...

  4. 64_n3

    nodejs-yamlish-0.0.5-9.fc26.noarch.rpm 11-Feb-2017 16:48 11966 nodejs-yargs-3.2.1-6.fc26.noarch.rpm ...

  5. Python设计模式中单例模式的实现及在Tornado中的应用

    单例模式的实现方式 将类实例绑定到类变量上 class Singleton(object): _instance = None def new(cls, *args): if not isinstan ...

  6. 关于IdByName 为什么一个消息主题要有 Id和 Name的解释

  7. 渗透常用SQL注入语句合集

    1.判断有无注入点; and 1=1 and 1=2 2.猜表一般的表的名称无非是admin adminuser user pass password 等..and 0<>(select ...

  8. css3 box-sizing属性值详解

    box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,border和padding不计算入widt ...

  9. CentOS7.6安装screenfetch

    方法1:yum install -y gitcd /usr/local/srcgit clone https://github.com/KittyKatt/screenFetch.gitcp scre ...

  10. bzoj 1110 贪心 + 进制转换

    思路:感觉脑洞好大啊... 因为每两个砝码其中一个都是另一个的倍数,我们可以知道砝码的种数很少,我们将所有容器的 容量都转换成用这些砝码的重量的进制表示,然后将所有砝码排序,然后贪心地取,取到不能再取 ...