请问各位谁有jsp session 购物车代码 ,可以发给我不了,。谢谢了

解决方案 »

  1.   

    /*
    Copyright by suoxin in 2003 to example in j2ee
    HhShop 购物车*/
    package database;
    public class ShopCar implements Comparable
    {
        private String name = null ;
        private float price = 0.0F ;
        private int  number = 0 ;
        public ShopCar (String name,float price,int number)
        {
            this.name = name;
            this.price = price;
            this.number = number;
        }
        public String getName()
        {
            return this.name;
        }
        public float getPrice()
        {
            return this.price;    
        }
        public int getNumber()
        {
            return this.number;    
        }
        /*
         @you must write this method ,to become implement comparable
        **/
        public int compareTo(Object o)
        {
            ShopCar n = (ShopCar )o;
            int lastCam = name.compareTo(n.name);
            return lastCam;
        }
    }
    在测试中,为了大家理解就用提交的方式购物
    Car.jsp 
    复制内容到剪贴板 
    代码:
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="database.ShopCar"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>添加大类</title>
    </head>
    <body>
    <%
    //判断是否提交
    String action = request.getParameter("action");
    if(action == null || action.equals("")){
    %>
    <form name="myform" method="post" action="url.jsp?action=add">
      <table width="600" border="1" align="center">
        <tr align="center">
          <td colspan="2">添加商品大类</td>
        </tr>
        <tr>
          <td width="121">大类名称:</td>
          <td width="463"><input type="text" name="name"></td>
        </tr>
        <tr>
          <td width="121">单价:</td>
          <td><input type="text" name="price"></td>
        </tr>
        <tr>
          <td width="121">数量</td>
          <td><input type="text" name="number"></td>
        </tr>
        <tr align="center">
          <td height="17" colspan="2"><input type="submit" name="Submit" value="提交"></td>
        </tr>
      </table>
    </form>
    <%
    }else{
    String name = request.getParameter("name").trim();
    String getPrice = request.getParameter("price").trim();
    float price =  Float.parseFloat(getPrice);
    int number = Integer.parseInt(request.getParameter("number").trim());
    ShopCar shop = new ShopCar(name,price,number);
    //建立一个购物对象
    java.util.ArrayList car = null;
    java.lang.Object object = session.getAttribute("car");
    if(object != null){
    car = (java.util.ArrayList)object;
    }else{
    car = new java.util.ArrayList();
    }
    car.add(shop);//把购物加到ArrayList对象中
    session.setAttribute("car",car);//存贮到session
    session.setMaxInactiveInterval(1800);
    out.println("提交成功,<a href='Car.jsp'>返回</a>继续");
    out.println("提交成功,<a href='showCar.jsp'>察看</a>继续");
    }
    %>
    </body>
    </html>