标题:购物车中的将商品加入购物车出错
只看楼主
猪猪00
Rank: 1
等 级:新手上路
帖 子:8
专家分:7
注 册:2016-4-16
结帖率:0
 问题点数:0 回复次数:1 
购物车中的将商品加入购物车出错
我觉得没有什么问题,但是就是出现了问题



程序代码:

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

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(request.getParameter("action")!=null){
            this.action = request.getParameter("action");
            if(action.equals("add"))//如果是添加商品进购物车
            {
                if(addToCart(request,response)){
                    request.getRequestDispatcher("/success.jsp").forward(request, response);
                }else{
                    request.getRequestDispatcher("/failure.jsp").forward(request, response);                    
                }
            }
            if(action.equals("show"))//如果是显示购物车商品
            {
//                showCart(request,response);
            }
        }
    }

    
    //添加商品到购物车
    private boolean addToCart(HttpServletRequest request, HttpServletResponse response)
    {
        String id = request.getParameter("id");
        String number = request.getParameter("num");
        Items item = idao.getItemsById(Integer.parseInt(id));
        
        //如果是第一次添加商品到购物车,就要在session中创建一个购物车类
        if(request.getSession().getAttribute("cart")==null){
            Cart cart = new Cart();
            request.getSession().setAttribute("cart", cart);
        }
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        if(cart.addItems(item, Integer.parseInt(number.trim()))){
            return true;
        }
        else{
            return false;
        }
    }




[此贴子已经被作者于2016-4-16 15:20编辑过]

搜索更多相关主题的帖子: request called method 购物车 
2016-04-16 15:15
猪猪00
Rank: 1
等 级:新手上路
帖 子:8
专家分:7
注 册:2016-4-16
得分:0 
自己解决了,jsp页面上面的JavaScript代码的问题,JavaScript代码定义的方法带参数,在超链接上调用的时候没有传参数,好吧,细节决定成败!
2016-04-16 18:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-463715-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.137746 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved