购物车(一)展示商品,以及将商品加入购物车,jdbc连接oracle数据库(用连接池)

功能描述

前期准备

(1)应用Oracle数据库

(2)向数据库中添加商品(从页面添加到数据库)

(3)将商品展示到jsp页面

(4)当用户点击“购买”时,将该商品添加到购物车

购物车操作

(1)创建购物车

(2)将商品放入购物车

(3)当用户点击同一个商品时,使购物车中的对应商品自动加1

(4)用户可以修改商品的数量

(5)用户可以清除购物车中的某个商品

(6)购物车自动计算每一种商品的价格

(7)购物车自动计算购物车中所有商品的总价格

(8)用户可以清空购物车中的所有商品

(1)在Oracle数据库创建商品表格
商品编号,商品名称,商品数量(不是购物车中的,而是数据库中的),原价,现价,图片
create table produce(
       proid int primary key,   
       proname varchar2(50),
       procount int,
       oldprice number,
       nowprice number,
       address varchar2(30),
       img varchar2(100)
)
数据库底层操作(即用jdbc连接数据库)
1)存放键值对的文件
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
userpass=tiger
(2)在db包中用DBHelper连接Oracle数据库
package com.ruide.db;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBHelper {
    public static ComboPooledDataSource ds;//oracle数据库连接池类

    static{
        ds=new ComboPooledDataSource();//创建连接池对象
        InputStream in=DBHelper.class.getClassLoader().getResourceAsStream("db.properties");
        Properties pro=new Properties();
        try {
            pro.load(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String driver=pro.getProperty("driver");
        String url=pro.getProperty("url");
        String username=pro.getProperty("username");
        String userpass=pro.getProperty("userpass");

        try {
            ds.setDriverClass(driver);
        } catch (PropertyVetoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ds.setJdbcUrl(url);
        ds.setUser(username);
        ds.setPassword(userpass);
    }
}
在db包中用DBManager封装结果集
package com.ruide.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruide.mapper.IMapper;

public class DBManager{
    Connection conn=null;
    PreparedStatement pstm=null;
    ResultSet rs=null;
    public Connection getconn()throws SQLException{
        //返回连接池对象
        return DBHelper.ds.getConnection();
    }
    //1增删改函数
    public int executeUpdate(String sql,Object[]params)throws SQLException{
        //建连接
        try {
            conn=this.getconn();
            //建预处理通道
            pstm=conn.prepareStatement(sql);
            if(params!=null){
                for (int i = 0; i < params.length; i++) {
                    pstm.setObject(i+1, params[i]);
                }
            }
            //执行sql
            int result=pstm.executeUpdate();
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }finally{
            try {
                pstm.close();
                conn.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //2对象查询函数
    public List executeQueryObjectList(String sql,Object[]params,IMapper map)throws SQLException{
        try {
            //建连接
            conn=this.getconn();
            //建通道
            pstm=conn.prepareStatement(sql);
            //sql语句绑定数据
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    pstm.setObject(i+1, params[i]);
                }
            }
            //得到结果集
            rs=pstm.executeQuery();
            //封装结果集
            List list=map.mapper(rs);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }finally{
            try {
                rs.close();
                pstm.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //3总条数查询
    public int executeTotal(String sql,Object[]params)throws SQLException{
        try {
            //建连接
            conn=this.getconn();
            //建预处理通道
            pstm=conn.prepareStatement(sql);
            //sql语句绑定数据
            if(params!=null){
                for (int i = 0; i < params.length; i++) {
                    pstm.setObject(i+1, params[i]);
                }
            }
            //得到结果集
            rs=pstm.executeQuery();
            //处理结果集
            rs.next();
            int total=rs.getInt(1);
            return total;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }finally{
            try {
                rs.close();
                pstm.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //4.List集合的 数组方式封装结果集
    public List executeQueryArrayList(String sql,Object[] params)throws SQLException{
        try {
            conn=this.getconn();
            pstm=conn.prepareStatement(sql);
            if(params!=null){
                for (int i = 0; i < params.length; i++) {
                    pstm.setObject(i+1, params[i]);
                }
            }
            rs=pstm.executeQuery();
            ResultSetMetaData rsmd=rs.getMetaData();
            int count=rsmd.getColumnCount();
            List list=new ArrayList();
            while(rs.next()){
                Object[] obj=new Object[count];
                for (int i = 0; i < obj.length; i++) {
                    obj[i]=rs.getObject(i+1);
                }
                list.add(obj);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }finally{
            try {
                rs.close();
                pstm.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //5.List集合的  HashMap方式封装结果集
    public List> executeQueryMapList(String sql,Object []params)throws SQLException{
        try {
            conn=this.getconn();
            pstm=conn.prepareStatement(sql);
            if(params!=null){
                for (int i = 0; i < params.length; i++) {
                    pstm.setObject(i+1, params[i]);
                }
            }
            rs=pstm.executeQuery();
            ResultSetMetaData rsmd=rs.getMetaData();
            int count=rsmd.getColumnCount();
            List> list=new ArrayList>();
            while(rs.next()){
                Map map=new HashMap();
                for (int i = 0; i < count; i++) {
                    String name=rsmd.getColumnName(i+1);
                    Object values=rs.getObject(name);
                    map.put(name,values);
                }

            list.add(map);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }finally{
            try {
                rs.close();
                pstm.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //6.二维数组方式封装数据
    public Object[][] executeQueryArray(String sql,Object []params)throws SQLException{
        try {
            List list=this.executeQueryArrayList(sql, params);
            Object[][] obj=new Object[list.size()][];
            for(int i=0;iget(i);
            }
            return obj;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw e;
        }finally{
            try {
                rs.close();
                pstm.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
DBManager的对象查询是在mapper包中封装的结果集
mapper包中的代码如下

package com.ruide.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public interface IMapper {
    public List mapper(ResultSet rs)throws SQLException;
}

实现接口
package com.ruide.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ruide.vo.Produce;

public class ProduceMapper implements IMapper{
    public List mapper(ResultSet rs)throws SQLException{
        List list=new ArrayList();
        while(rs.next()){
            Produce pro=new Produce();
            pro.setProid(rs.getInt("PROID"));
            pro.setProname(rs.getString("PRONAME"));
            pro.setProcount(rs.getInt("PROCOUNT"));
            pro.setOldprice(rs.getDouble("OLDPRICE"));
            pro.setNowprice(rs.getDouble("NOWPRICE"));
            pro.setAddress(rs.getString("ADDRESS"));
            pro.setImg(rs.getString("IMG"));
            list.add(pro);
        }
        return list;
    }
}

vo包,主要是javabean

package com.ruide.vo;

public class Produce {
    private int proid;
    private String proname;
    private int procount;//数据库中商品的数量
    private double oldprice;
    private double nowprice;
    private String address;
    private String img;
    private int buycount;//购物车中相同商品的数量
    private double prices;//计算购物车中每一种货物的价格

    public double getPrices() {
        return nowprice*buycount;
    }
    public void setPrices(double prices) {
        this.prices = prices;
    }

    public int getBuycount() {
        return buycount;
    }
    public void setBuycount(int buycount) {
        this.buycount = buycount;
    }



    public int getProid() {
        return proid;
    }
    public void setProid(int proid) {
        this.proid = proid;
    }
    public String getProname() {
        return proname;
    }
    public void setProname(String proname) {
        this.proname = proname;
    }
    public int getProcount() {
        return procount;
    }
    public void setProcount(int procount) {
        this.procount = procount;
    }
    public double getOldprice() {
        return oldprice;
    }
    public void setOldprice(double oldprice) {
        this.oldprice = oldprice;
    }
    public double getNowprice() {
        return nowprice;
    }
    public void setNowprice(double nowprice) {
        this.nowprice = nowprice;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getImg() {
        return img;
    }
    public void setImg(String img) {
        this.img = img;
    }

}

dao包,sql语句和数据,调用DBManager中的方法,传递sql语句和数据

package com.ruide.dao;

import java.sql.SQLException;
import java.util.List;

import com.ruide.db.DBManager;
import com.ruide.mapper.IMapper;
import com.ruide.mapper.ProduceMapper;
import com.ruide.vo.Produce;

public class ProduceDAO {
    //保存
    public int save(Produce p)throws SQLException{
        String sql="insert into produce values(?,?,?,?,?,?,?)";
        Object[] params={p.getProid(),p.getProname(),p.getProcount(),p.getOldprice(),p.getNowprice(),p.getAddress(),p.getImg()};
        DBManager db=new DBManager();
        int hang= db.executeUpdate(sql, params);
        return hang;
    }
    //删除
    public int delete(int proid)throws SQLException{
        String sql="delete from produce where proid=?";
        Object[] params={proid};
        DBManager db=new DBManager();
        return db.executeUpdate(sql, params);
    }


    //主键查询
    public Produce findById(int proid)throws SQLException{
        String sql="select *from produce where proid=?";
        Object[] params={proid};
        DBManager db=new DBManager();
        IMapper map=new ProduceMapper();
        List list=db.executeQueryObjectList(sql, params, map); 
        Produce pro=list.get(0);
        return pro;
    }
    //查询所有
    public List findAll()throws SQLException{
        String sql=" select *from Produce ";
        IMapper map=new ProduceMapper();
        DBManager db=new DBManager();
        List list=db.executeQueryObjectList(sql, null, map);
        return list;
    }
}
filter包:过滤器,过滤编码方式,防止出现乱码
package com.ruide.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EncodingFilter implements Filter{
    public void init(FilterConfig filterConfig)throws ServletException{}
    public void destroy(){}
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
            throws ServletException,IOException{
        HttpServletRequest request1=(HttpServletRequest) request;
        HttpServletResponse response1=(HttpServletResponse)response;
        request1.setCharacterEncoding("UTF-8");
        response1.setCharacterEncoding("UTF-8");
        chain.doFilter(request1, response1);
    }

}
底层已经建立好,下面开始做跟购物车相关的操作
添加商品的jsp页面和其所对应的servlet->用来向数据库添加数据
1.添加商品的jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'saveproduce.jsp' starting pagetitle>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    

  head>

  <body>
    <form action="saveproduce" method="post">
        <table border="1" align="center" cellpadding="0" cellspacing="0">
            <tr><th>添加商品th>tr>
            <tr><td>商品编号<input type="text" name="proid">td>tr>
            <tr><td>商品名称<input type="text" name="proname">td>tr>
            <tr><td>商品数量<input type="text" name="procount">td>tr>
            <tr><td>商品原价<input type="text" name="oldprice">td>tr>
            <tr><td>商品现价<input type="text" name="nowprice">td>tr>
            <tr><td>商品产地<input type="text" name="address">td>tr>
            <tr><td>商品图片<input type="text" name="img">td>tr>
            <tr><td>
                    <input type="submit" value="保存">
                    <input type="reset" value="重置">
            td>tr>
        table>
    form>

  body>
html>



2.其所对应的servlet->用来向数据库添加数据
package com.ruide.action;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruide.dao.ProduceDAO;
import com.ruide.vo.Produce;

public class SaveProduceAction extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
            }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            //获取控件的内容
            int proid=new Integer(request.getParameter("proid"));
            String proname=request.getParameter("proname");
            int procount=new Integer(request.getParameter("procount"));
            double oldprice=new Double(request.getParameter("oldprice"));
            double nowprice=new Double(request.getParameter("nowprice"));
            String address=request.getParameter("address");
            String img=request.getParameter("img");
            Produce p=new Produce();
            p.setProid(proid);
            p.setProname(proname);
            p.setProcount(procount);
            p.setOldprice(oldprice);
            p.setNowprice(nowprice);
            p.setAddress(address);
            p.setImg(img);
            ProduceDAO dao=new ProduceDAO();
            int hang=dao.save(p);
            //return hang;
            response.sendRedirect("showproduce");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}


//3.展示数据的servlet
package com.ruide.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruide.dao.ProduceDAO;
import com.ruide.vo.Produce;

public class ShowProduceAction extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //创建DAO对象调用查询方法
        try {
            ProduceDAO dao=new ProduceDAO();
            List<Produce> list=dao.findAll();
            //设置request范围
            request.setAttribute("list", list);
            //请求转发
            request.getRequestDispatcher("showproduce.jsp").forward(request, response);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
//4.展示数据的jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.ruide.vo.Produce"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'showproduce.jsp' starting pagetitle>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    

  head>

  <body>
    <table border="1"  align="center">
    <%
        List list=(List)request.getAttribute("list");
        for(int i=0;iget(i);
            if(i%3==0){
    %>
            <tr>
        <%
            }
        %>
        <td>
            <table border="0" cellpadding="0" cellspacing="5" >
                <tr>
                    <td>
                        <img width="200" src="<%=p.getImg()%>" >
                    td>
                tr>
                <tr>
                    <td>
                        商品名称:<%=p.getProname() %>
                    td>
                tr>
                 <tr>
                    <td>
                        原价:<s><%=p.getOldprice() %>s><br/>
                        现价:<font color="red"><b><%=p.getNowprice()%>b>font>
                    td>
                tr>
                <tr>
                    <td>
                        产地:<%=p.getAddress() %>
                    td>
                tr>
                <tr>
                    <td>
                        数量:<%=p.getProcount()%> 
                    td>
                tr>
                <tr>
                    <td><a href="probuy?proid=<%=p.getProid() %>">购物a>td>
                tr>
            table>
          td>

    <%
    }
     %> 
     <tr><td colspan="3" align="center"><a href="saveproduce.jsp">添加商品a>td>tr>
     table>
  body>
html>
核心代码:当用户点击“购买”时,将该商品添加到购物车
添加商品到购物车的servlet
package com.ruide.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ruide.dao.ProduceDAO;
import com.ruide.vo.Produce;

public class ProBuyAction extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            //1通过主键获取用户点击的商品对象
            int  proid=new Integer(request.getParameter("proid"));          
            //2.创建购物车
            HttpSession session=request.getSession();
            //解决第一次问题:用户来到后先获取购物车
            List<Produce> car=(List<Produce>)session.getAttribute("car");
            if(car==null){//第一次来没有购物车创建
                car=new ArrayList<Produce>();
                session.setAttribute("car", car);
            }
            //3.通过商品id得到一个具体的商品对象
            ProduceDAO  dao=new ProduceDAO();
            Produce p=dao.findById(proid);
            //将商品放入购物车,是数量加1,处理重复商品

            boolean iden=false;
            for(int i=0;i<car.size();i++){
                Produce pd=car.get(i);
                //用户点击的id  session中的id
                if(proid==pd.getProid()){
                    iden=true;
                    pd.setBuycount(pd.getBuycount()+1);
                    break;
                }
            }
            //第一次商品不存在,将商品添加到购物车
            if(iden==false){
                car.add(p);
                p.setBuycount(p.getBuycount()+1);

            }

            response.sendRedirect("showcar.jsp");
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

}

购物车展示页面jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.ruide.vo.Produce"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'showcar.jsp' starting pagetitle>
      
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
    <script type="text/javascript">
        function dochange(proid){
            var value=document.getElementById("buycount"+proid).value;
            window.location.href="updatebuycount?proid="+proid+"&buycount="+value;
        }
    script>
  head>

  <body>
  <table border="0" cellpadding="0" cellspacing="0" align="center" width="700">
    <tr>
        <th>商品图片th>
        <th>商品名称th>
        <th>商品价格th>
        <th>商品数量th>   
        <th>商品金额th>
        <th>删除th> 
    tr>
    <% List list=(List)session.getAttribute("car"); 
        for(int i=0;iget(i);

    %>
    <tr>
        <td>
            <img width="200" src="<%=p.getImg()%>" >
        td>
        <td>
            <%=p.getProname() %>
        td>
        <td>
            <s><%=p.getOldprice() %>s><br/>
            <font color="red"><b><%=p.getNowprice() %>b>font>
        td>
<%-- 方法一:       
"updatebuycount" method="post"> "hidden" name="proid" value="<%=p.getProid() %>"> <input type="text" size="1" name="buycount" value="<%=p.getBuycount() %>"> <input type="submit" value="修改" > td> form> --%> <td> <input type="text" size="1" id="buycount<%=p.getProid()%>" value="<%=p.getBuycount() %>" / > <input type="button" onclick="dochange(<%=p.getProid() %>)" value="修改" /> td> <td><%=p.getPrices()%> td> <td> <a href="deleteproduce?proid=<%=p.getProid() %>" onclick="dodelet()">删除a> td> tr> <% } %> <tr> <td colspan="6" align="center"> <% int zong=0; //List car=(List)session.getAttribute("car"); for(int i=0;iget(i); zong+=p.getNowprice()*p.getBuycount(); %> <% } %> <b>总金额:<font color="red"><%=zong%>font>b> td> form> tr> <tr> <td colpan="6"> <a href="deletecar">清空购物车a>   <a href="showproduce">继续购物a> td> tr> table> body> html> 用户修改商品数量的servlet package com.ruide.action; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.ruide.vo.Produce; public class UpdateBuyCountAction extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int proid=new Integer(request.getParameter("proid")); int buycount=new Integer(request.getParameter("buycount")); HttpSession session=request.getSession(); List<Produce> car=(List<Produce>)session.getAttribute("car"); for(int i=0;i<car.size();i++){ Produce p=car.get(i); if(p.getProid()==proid){ p.setBuycount(buycount); break; } } response.sendRedirect("showcar.jsp"); } } 用户删除购物车中某一商品的servlet package com.ruide.action; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.ruide.vo.Produce; public class DeleteProduceAction extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int proid=new Integer(request.getParameter("proid")); HttpSession session=request.getSession(); List<Produce> car=(List<Produce>)session.getAttribute("car"); for(int i=0;i<car.size();i++){ Produce p=car.get(i); if(p.getProid()==proid){ car.remove(p);//从session中移除对象 } } response.sendRedirect("showcar.jsp"); } } 用户清空购物车中所有商品的servlet package com.ruide.action; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.ruide.vo.Produce; public class DeleteCarAction extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); List<Produce> car=(List<Produce>)session.getAttribute("car"); car.clear(); response.sendRedirect("showcar.jsp"); } }

你可能感兴趣的:(java-web)