用JSP的Session機制編寫的購物車程序詳解
用JSP的Session 機制編寫的程序就可以是你擁有一個功能強大購物車程序,是不是很誘人呢?趕緊開始我們的程序吧
JSP Session 機制購物車之一構建的商品類
◆寫一個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;
- }
- }
JSP Session 機制購物車之二購物車實現
◆首先建立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("<script>alert('你已經訂購了此商品!');window.close();< SPAN>script>");
- }
◆如果購入商品不存在,則直接將商品添加到ArrayList對象當中,并寫入 session
- else
- {
- ay.add(goods);
- session.setAttribute("car",ay);
- response.sendRedirect("order_index.jsp");
- }
- }
- }
- }
- %>
JSP Session 機制購物車之三刪除商品
◆對購物車中的商品進行刪除操作
◆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"));
◆創(chuàng)建符合條件參數要刪除的商品對象
- Goods goods = new Goods(id,name,price,number);
◆獲取session 中存儲的ArrayList對象
- ArrayList ay = (ArrayList)session.getAttribute("car");
- Iterator it = ay.iterator();
◆遍歷ArrayList對象,并將ArrayList對象中的元素和創(chuàng)建的符合參數條件要刪除的商品進行比較
- 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 機制購物車是不是使你眼睛豁然一亮的感覺呢?趕緊開始吧,JSP Session機制的使用期待你的嘗試。
【編輯推薦】