添加购物车功能全部代码

1.添加购物车功能
[1]





冬瓜


商城价:¥299.00


        <div class="col-md-2">
            <a href="${ pageContext.request.contextPath }/cart/product_info2.htm">
                <img src="products/1/cs10002.jpg" width="170" height="170" style="display: inline-block;">
            </a>
            <p><a href="${ pageContext.request.contextPath }/cart/product_info2.htm" style='color:green'>圆白菜</a></p>
            <p><font color="#FF0000">商城价:&yen;299.00</font></p>
</div>

[2]




                <div class="col-md-6">
                    <div><strong></strong></div>
                    <div style="border-bottom: 1px dotted #dddddd;width:350px;margin:10px 0 10px 0;">
                        <div>编号:751</div>
                    </div>

                    <div style="margin:10px 0 10px 0;">亿家价: <strong style="color:#ef0101;">¥:4.78元/份</strong> 参 考 价: <del>¥6.00元/份</del>
                    </div>

                    <div style="margin:10px 0 10px 0;">促销: <a target="_blank" title="限时抢购 (2014-07-30 ~ 2015-01-01)" style="background-color: #f07373;">限时抢购</a> </div>

                    <div style="padding:10px;border:1px solid #e7dbb1;width:330px;margin:15px 0 10px 0;;background-color: #fffee6;">
                        <div style="margin:5px 0 10px 0;">白色</div>

                        <div style="border-bottom: 1px solid #faeac7;margin-top:20px;padding-left: 10px;">购买数量:
                            <input id="quantity" name="quantity" value="1" maxlength="4" size="10" type="text"> </div>

                        <div style="margin:20px 0 10px 0;;text-align: center;">
                            <a href="/day17/CartServlet?name=大冬瓜">
                                <input style="background: url('/day11/demo2/images/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0);height:36px;width:127px;" value="加入购物车" type="button">
                            </a> &nbsp;收藏商品</div>
                    </div>

[3]



//src那要改路径,把上面照片复制

                <div class="col-md-6">
                    <div><strong>小南瓜</strong></div>
                    <div style="border-bottom: 1px dotted #dddddd;width:350px;margin:10px 0 10px 0;">
                        <div>编号:751</div>
                    </div>

                    <div style="margin:10px 0 10px 0;">亿家价: <strong style="color:#ef0101;">¥:4.78元/份</strong> 参 考 价: <del>¥6.00元/份</del>
                    </div>

                    <div style="margin:10px 0 10px 0;">促销: <a target="_blank" title="限时抢购 (2014-07-30 ~ 2015-01-01)" style="background-color: #f07373;">限时抢购</a> </div>

                    <div style="padding:10px;border:1px solid #e7dbb1;width:330px;margin:15px 0 10px 0;;background-color: #fffee6;">
                        <div style="margin:5px 0 10px 0;">白色</div>

                        <div style="border-bottom: 1px solid #faeac7;margin-top:20px;padding-left: 10px;">购买数量:
                            <input id="quantity" name="quantity" value="1" maxlength="4" size="10" type="text"> </div>

                        <div style="margin:20px 0 10px 0;;text-align: center;">
                            <a href="/day17/CartServlet?name=圆白菜">       //要改路径
                                <input style="background: url('/day11/demo2/images/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0);height:36px;width:127px;" value="加入购物车" type="button">
                            </a> &nbsp;收藏商品</div>
                    </div>

[4]
public class CartServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    // 1. 获取购物车对象: Map集合 (购物车保存在session)
    // 获取session,在浏览器中获取会话技术
    HttpSession session = request.getSession();
    //强转成Map集合,不强转的话,对于集合来说就是object类型,但是map集合的键是string类型,需要强转成map集合,map的key是商品名称,value是数量
    Map<String, Integer> map = (Map<String, Integer>) session.getAttribute("cart");
    // 2. 判断购物车是否存在
    if (map == null) {
        // 购物车不存在,就创建一个购物车,new一个就可以了
        map = new LinkedHashMap<String, Integer>();
    }
    // 购物车已存在 Map集合<String 商品名称, Integer数量>
    // 3. 判断是否购买了该商品
    // 3.1 获取购买商品名称
    String name = request.getParameter("name");
    //商品名称是中文的,而且是get提交方法,  <a href="/day17_/CartServlet?name=大冬瓜">  跳转页面链接里面有?,如果是post提交,就没有?啥的
    name = new String(name.getBytes("iso-8859-1"), "UTF-8");
    if (map.containsKey(name)) {
        // 已经购买了该商品,代表map中已经有商品了,如果有商品就把map中商品数量加1,存入集合中
        Integer count = map.get(name);
        //如果有商品就统计购买的商品的数量
        count++;
        map.put(name, count);  //key是商品名称,value是商品统计购买的数量
    } else {
        // 第一次购买该商品,map中没有商品,如果购物车没有该商品,就将该商品添加到集合中,并且数量为1.
        map.put(name, 1);
    }

    //4. 将购物车信息,保存到session中
    session.setAttribute("cart", map);

    //5. 跳转页面
    response.getWriter().println("<h3><a href='/day17_/cart/product_list.jsp'>继续购物</a> | <a href='/day17_/cart/cart.jsp'>去结算</a></h3>");

}

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

}

猜你喜欢

转载自blog.csdn.net/pf503603/article/details/82220658