cookie商品浏览记录

来源:互联网 发布:淘宝卖家旺旺设置 编辑:程序博客网 时间:2024/05/17 02:59
package cookie;


import java.io.IOException;
import java.io.PrintWriter;


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.MyCookieUtil;


public class ProductServlet extends HttpServlet {


public ProductServlet() {
super();
}



public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}




public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求参数目的:存入到cookie中
String id = request.getParameter("id");
//先获取所有的cookie,查找指定名称的cookie
Cookie[] cookies = request.getCookies();
//查找指定名称的cookie
Cookie cookie = MyCookieUtil.getCookieByName(cookies, "product");
//如果cookie==null,我第一次访问,创建cookie,回写
if(cookie == null){
//我第一次访问,创建cookie,回写
Cookie c = new Cookie("product",id);
//回写
response.addCookie(c);
}else{
String value = cookie.getValue();
//判断,当前的商品的id是否包含在value中
String[] values = value.split(",");
if(!(checkId(values,id))){
//不包含
cookie.setValue(value+","+id);
//回写
response.addCookie(cookie);
}
}

//重定向到商品页面
response.sendRedirect("/day11/cookie/productList.jsp");

}
private boolean checkId(String[] values,String id){
for(String s:values){
if(s.equals(id)){
return true;
}
}
return false;
}

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


doGet(request,response);
}


public void init() throws ServletException {
// Put your code here
}


}




<%@page import="utils.MyCookieUtil"%>
<%@ 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 'productList.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">
-->
<style type="text/css">
.img1{
width: 160px;
height: 140px;
}
</style>
  </head>
  
  <body>
   <img class="img1" src="/day11/img/1.jpg"><a href="/day11/product?id=1">11</a>
   <img class="img1" src="/day11/img/2.jpg"><a href="/day11/product?id=2">22</a>
   <img class="img1" src="/day11/img/3.jpg"><a href="/day11/product?id=3">33</a><br>
   <img class="img1" src="/day11/img/4.jpg"><a href="/day11/product?id=4">44</a>
   <img class="img1" src="/day11/img/5.jpg"><a href="/day11/product?id=5">55</a>
   <img class="img1" src="/day11/img/6.jpg"><a href="/day11/product?id=6">66</a>
   <h3>浏览记录</h3>
   <%
    //获取cookie中value1,2,3循环遍历
    Cookie[] cookies = request.getCookies();
    Cookie cookie = MyCookieUtil.getCookieByName(cookies, "product");
    if(cookie != null){
    //获取1,2,3
    String value = cookie.getValue();
    //分隔
    String[] ids = value.split(",");
    for(String id : ids){
%>
<img  class="img2" src="/day11/img/<%=id%>.jpg">
<%     }
    }
    %>
  </body>
</html>

0 0
原创粉丝点击