数据流程:页面上是商品列表,点击<a href="productServlet">商品名</a>
==》跳转到自定义的servlet中进行处理,先得到请求的参数id;
==》获取所有cookies =requst.getCookies()方法。
==》根据名称查找cookie是否存在 。通过循环cookies for(Cookie c:cookies) if(c.getName().eques("product")) 来确定cookie是否为null
==》也就确定是否是第一次访问,如果是第一次访问,新建cookie对象 Cookie cookie=new Cookie("product",id); 回写cookie response.addCookie(cookie);
==》如果存在,先判断是否包含请求的商品编号 if(不包含) cookie.setValue(productIds + "," + id); 回写cookie response.addCookie(cookie);
<%@pageimport="utils.MyUtils"%>
<%@ page language ="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<meta http-equiv= "Content-Type" content ="text/html; charset=UTF-8">
<title> Insert title here</ title>
<style type= "text/css">
.imga{
width:160px;
height:140px;
}
.imgb{
width:80px;
height:70px;
}
</style>
</head>
<body>
<img class="imga" src= "/JSP/img/1.jpg"><a href= "../product?id=1">手电筒 </a >
<img class="imga" src= "/JSP/img/2.jpg"><a href= "../product?id=2">电话</a >
<img class="imga" src= "/JSP/img/3.jpg"><a href= "../product?id=3">电视</a ><br />
<img class="imga" src= "/JSP/img/4.jpg"><a href= "../product?id=4">冰箱</a >
<img class="imga" src= "/JSP/img/5.jpg"><a href= "../product?id=5">手表</a >
<img class="imga" src= "/JSP/img/6.jpg"><a href= "../product?id=6">笔记本电脑 </a >
<hr>
<h3> 浏览记录</h3 >
<%
//获取request.cookie
Cookie[] cookies=request.getCookies();
Cookie cookie=MyUtils.getCookieByName(cookies, "product");
if(cookie!=null )
{
String values=cookie.getValue();
String [] ids=values.split( ",");
for(String id:ids)
{
%>
<img class ="imgb" src="/JSP/img/<%= id %> .jpg"><br />
<%
}
}
%>
</body>
</html>
package Cookie_Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.MyUtils;
/**
* 浏览记录后台
*
* @author sunyb
*
*/
public class ProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 1.获取请求参数 2.获取cookie数组;通过指定名查找自己定义 的coockie 3. 如果cookie为null 则说明是
* 第一次访问 4 .否则 不是第一次访问
*/
// 获取请求参数 目的,存入到cookie 中
String id = request.getParameter( "id");
// 先获取所有的cookie,查找指定名称的cookies数组
Cookie[] cookies = request.getCookies();
// 查找指定名称的cookie
Cookie cookie = MyUtils. getCookieByName(cookies, "product");
// 如果cookie==null,我第 一次访问,创建cookie,回写
if (cookie == null) {
// 我是第一次访问,创建cookie,回写
Cookie c = new Cookie( "product", id);
// 回写
response.addCookie(c);
} else {
// 如果不是第一次访问
// 获取cookie的value值 ,可能会为 1,2,3
String productIds = cookie.getValue();
String[] values = productIds.split( ",");
if (!checkId(values, id)) {
cookie.setValue(productIds + "," + id);
response.addCookie(cookie);
}
}
// 重定向到商品页面
response.sendRedirect( "/JSP/cookies/productList.jsp");
}
/**
* 判断当前的 id是否存在到cookies中
*
* @param ids
* cookies中存在的浏览过的商品id
* @param value
* 当前访问的商品id
* @return 包含到原来的cookies中返回true,否则返回false
*/
private boolean checkId(String[] ids, String value) {
for (String tem : ids) {
if (tem.equals(value))
return true;
}
return false;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}