<%@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>
作用范围为从浏览器打开到浏览器关闭