将商品添加到购物车(session、map)

<%@page import="utils.cookieutils"%>
<%@ 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 'MyJsp.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>
	<div>
		<a href="/J2EE/html/maoyi.jsp">毛衣</a>
	</div>
	<!-- 获得当前工程名 -->
	<div>
		<a href="/J2EE/html/mianao.jsp">棉袄</a>
	</div>
<% 
 Cookie [] cookies= request.getCookies();
 Cookie cookie=cookieutils.findcookie(cookies,"history");
 if(cookie==null){//没有浏览记录
 %>
		<h3>你没有浏览过商品</h3>
		<% }
 else{
 String value = cookie.getValue();
 String[] ids = value.split("-");//将id们构成的字符串分割成数组
 for(String id:ids){
 %>
		<li><%=id%></li>
		<%
 }
 }
 %>
	<h1><a href="${pageContext.request.contextPath}/clearservlet">清除浏览记录</a></h1>
</body>
</html>
<%@ 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 'maoyi.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>
	This is a maoyi.
	<br>
	<a href="/J2EE/cartservlet?name=miaoyi">加入购物车</a>
</body>
</html>

<%@ 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 'mianao.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>
	This is a mianao
	<br>
	<a href="/J2EE/cartservlet?name=mianao">加入购物车</a>
</body>
</html>

package javapack;

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");//接收商品名称
		Map<String,Integer>map=(Map<String, Integer>)request.getSession().getAttribute("cart");
		if(map==null){
			//创建集合MAP用于保存购物信息,MAP的key是商品名称,MAP的value是商品数量
			map=new LinkedHashMap<String,Integer>();
		}
		//判断购物车是否买了该商品
		if(map.containsKey(name)){//map中已经有该商品
			Integer count=map.get(name)+1;
			map.put(name, count);
		}
		else{//如果购物车没有买该商品,将商品添加到map中,数量为1
			map.put(name, 1);
			//如果购物车中已有该商品,获得map中该商品的数量加1,存入到MAP集合中
		}
		//将map集合保存到session中
		request.getSession().setAttribute("cart", map);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println("<h3><a href='/J2EE/productlist.jsp'>继续购物</a>|<a href='/J2EE/html/cart.jsp'>去结算</a></h3>");
	}

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

		doGet(request,  response);

	}

}
<%@ 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 name:map.keySet()){
  Integer count= map.get(name);
  %>
	<h4>
		你购买的商品是<%=name %>,购买的数量是<%=count %></h4>
	<%
  }
   %>
</body>
</html>

作用范围为从浏览器打开到浏览器关闭


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值