cookie商品浏览记录

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值