将商品添加到购物车

CartServlet:
package bull03.Cart;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

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

public class CartServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //接收商品名称
        String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        System.out.println(name);
        //创建map集合用于保存购物信息,key是商品,value是购买的数量
        Map<String,Integer> map = (Map<String, Integer>) request.getSession().getAttribute("cart");
        if(map == null) {
            //如果为空,创建一个map集合
            map = new LinkedHashMap<String, Integer>();
        }

        //如果map中包含名称为name的商品
        if(map.containsKey(name)) {
            //获取name对应的value值:商品数量
            Integer count = map.get(name);
            //对数量+1并写回map中
            count++;
            map.put(name, count);
        } else {
            //如果map中没有name,那么把name写进去
            map.put(name, 1);
        }

        //将map集合保存到session中,命名为cart
        request.getSession().setAttribute("cart", map);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("<h3><a href='/Web11/cart/product_list.jsp'>继续购物</a>|<a href='/Web11/cart/cart.jsp'>进行结算</a></h3>");
    }


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

        doGet(request,response);
    }

}
product_list.jsp:
<div class="col-md-2">
                <a href="${pageContext.request.contextPath }/ProductServlet?id=1">
                    <img src="/Web11/history/products/1/cs10001.jpg" width="170" height="170" style="display: inline-block;">
                </a>
                <p><a href="/Web11/cart/product_info.htm" style='color:green'>冬瓜</a></p>
                <p><font color="#FF0000">商城价:&yen;299.00</font></p>
            </div>
(判断是否登录)
        <%
                User existUser = (User)request.getSession().getAttribute("existUser");
                if(existUser == null) {
                %>
                <h2>您还没登录!请<a href="/Web11/cart/login.htm">登录</a></h2>  
            <% 
                } else {
             %>
             ...
             ...
             ...
             <%
              }
          %>
product_info.htm:
    <div style="margin:20px 0 10px 0;;text-align: center;">
                                <a href="/Web11/CartServlet?name=111">
                                    <input style="background: url('./images/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0);height:36px;width:127px;" value="加入购物车" type="button">
                                </a> &nbsp;收藏商品</div>
                        </div>
cart.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'cart.jsp' starting page</title>

    <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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <h1>购物车列表</h1>
    <%
        Map<String,Integer> map = (Map<String,Integer>)request.getSession().getAttribute("cart");
        for(String product:map.keySet()) {
            //获取购物车商品数量,一个product输出一次
            Integer count = map.get(product);
     %>
        <h3>您购买的商品是:<%=product %>  商品数量为:<%=count %></h3>
     <%
        }
      %>
  </body>
</html>
  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值