要用JSP+javabean+severlet做的
简单点就好了,没做过,不知道什么原理,无从下手

解决方案 »

  1.   

    简单因该创建一个购物车Bean 里面2个属性。。一个是商品。一个是数量
    然后在Servlet中 把商品放进去。。在New个Map 把商品ID 当Key, 购物车Bean 当值放入Map中
    放入之前要查询 Map里是否已经有这是商品ID, 如果有则 数量+1 如果没有则放入Map中。
    最后在jsp页面中显示 Map中的值。。
      

  2.   


       //大概是这样的:
        String id = request.getpara....("id");
        if (session.getsetAttribute("car") == null) { //用户当前不存在购物车
            session.setAttribute(new HashMap());       // 为用户创建一个购物车,map中的key存放商品的唯一标识(ID),value则是商品的javaBean
       }
           Map car = (Map)session.getAttribute("car");
          if (car.get(id)) {  // 如果这个商品已经存在于购物车
          //则在商品数量上面+1
      } else {
        // 如果这个商品在购物车中不存在,则查出商品信息,添加到Map
      }
          
    不是完整代码,还是楼主自己写完代码,上面是流程了原理
      

  3.   

    发个Servlet LZ看下
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    HttpSession session =request.getSession(false);
    RequestDispatcher dispatcher;
    Map cart=(Map)session.getAttribute("cart");//从session中获得购物车
    FoodBean food=(FoodBean)session.getAttribute("foodToAdd");//从session中获得要加入购物车的商品

    if(cart==null){//判断 购物车是否为空。。如果为空则创建。并放入session
    cart=new HashMap();
    session.setAttribute("cart", cart);
    }


    CartItemBean cartItem=(CartItemBean)cart.get(food.getFoodID());//判断map中是否有要添加的商品
    ifcartItem!=null){
    cartItem.setQuantity(cartItem.getQuantity()+1);//有+1
    }else
    cart.put(food.getFoodID(), new CartItemBean(food,1));//没有则放入。
    dispatcher=request.getRequestDispatcher("/ch04/shopCart.jsp");//页面跳转
    dispatcher.forward(request, response);
    }
      

  4.   

    再发个显示页面部分代码
    <%
    Map cart=(Map)session.getAttribute("cart");//取出map
    if(cart==null||cart.size()==0)
    out.print("<p>购物车当前为空!</p>");
    else{
    Set cartItems=cart.keySet();
    Object[] foodItems=cartItems.toArray();
    double total=0;
    for(int i=0;i<foodItems.length;i++){// 循环Map中的值(之前要用到Set集合)
    CartItemBean cartItem=(CartItemBean)cart.get((String)foodItems[i]);
    FoodBean food=cartItem.getFood();
    int quantity=cartItem.getQuantity();
    double price=food.getFoodPrice();
    double subtotal=quantity*price;
    total+=subtotal;%>
      

  5.   

    LZ去参考下吧,代码很全的,包括表的结构都存在。
    http://hi.baidu.com/cjhnihao/blog/item/78dc95ec5d65db2262d09fe4.html