一、构建的商品类

//写一个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. python自动开发之(算法)第二十七天

    1.什么是算法? 算法(Algorithm):一个计算过程,解决问题的方法 2.复习:递归 递归的两个特点:(1) 调用自身 (2)结束条件 def func1(x): print(x) func1( ...

  2. [node.js] async/await如何优雅处理异常?

    node.js的世界,从callback开始,不会止于async. 所有人都在骂为什么不能完全进化,其实我感觉这就是老外的细心,为了承上.这也就是为什么async其实就是promise一样,假如不是一 ...

  3. 设置Git远程仓库

    1,注册一个GitHub账户,登陆GitHub账户,添加一个储存库 2,进入Ubuntu命令窗口,创建文件夹.如   mkdir   git echo "# first_git" ...

  4. LFM隐语义模型Latent Factor Model

    实际应用 LFM 模型在实际使用中有一个困难,就是很难实现实时推荐.经典的 LFM 模型每次训练都需要扫描所有的用户行为记录,并且需要在用户行为记录上反复迭代来优化参数,所以每次训练都很耗时,实际应用 ...

  5. mktime(将时间结构数据转换成经过的秒数)

    mktime(将时间结构数据转换成经过的秒数)表头文件#include<time.h>定义函数time_t mktime(strcut tm * timeptr);函数说明mktime() ...

  6. HDU 2859 Phalanx(二维DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 题目大意:对称矩阵是这样的矩阵,它由“左下到右”线对称. 相应位置的元素应该相同. 例如,这里是 ...

  7. lr获取响应结果中的乱码并转成中文

    {,"message":"楠岃瘉鐮侀敊璇\xAF","developerMessage":"楠岃瘉鐮侀敊璇\xAF"} ...

  8. Join 与 CountDownLatch 之间的区别

    Join 与 CountDownLatch 之间的区别 import java.util.concurrent.CountDownLatch; public class CountDownLatchT ...

  9. EditText属性描述

    android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint=&qu ...

  10. java 用maven 构建项目时@Override错误的解决办法

    把工程编译时使用JDK1.6以上版本可以解决. eclipse中 Preferences-->Java-->Compiler-->Configure Project Specific ...