利用Cookie显示最近浏览的商品

  功能描述:保存用户最近浏览的5件商品。

  功能实现:一个商品列表页面list.jsp,一个商品详情页面detail.jsp,用户在商品列表页,点击进入商品详情页,后台自动将其显示在最近浏览的商品中。

  list.jsp:

  显示最近浏览的5件商品

  1). 获取所有的cookie

  2).从中筛选出商品的cookie:

          如果cookie的name符合GOODS开头的即符合条件

  3).显示cookie的value

  detail.jsp

  把商品的信息以cookie方式传回给浏览器,删除一个cookie

  1).确定要被删除的cookie:GOODS开头的cookie数量大于等于5,且若从list.jsp页面传入的book不在GOODS中则删除较早的那个cookie,若在其中,则删除该cookie

  2).把从list.jsp传入的商品作为cookie返回。

list.jsp:

<%@ 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>list</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>
    <a href="<%=basePath%>goods/detail.jsp?name=A">A</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=B">B</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=C">C</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=D">D</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=E">E</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=F">F</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=G">G</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=H">H</a><br>
    <a href="<%=basePath%>goods/detail.jsp?name=I">I</a><br>
    <%
      //列出所有以GOODS_开头的cookie
      Cookie[] cookies=request.getCookies();
    if(cookies !=null){
    for(int i=0;i<cookies.length;i++){
 	   if(cookies[i].getName().startsWith("GOODS_")){
 		   %>
 		   <%=cookies[i].getValue() %>
 		   <% 
 	   }
 	   }
    }
    %>
  </body>
</html>
detail.jsp:

<%@ 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>detail</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>
     <%String name=request.getParameter("name");  %>
    NAME:<%=name %>
    <!-- 检查cookie -->
    <%Cookie[] cookies=request.getCookies();
    //查出所有以GOODS_开头的cookie
       int sum=0;
       for(int i=0;i<cookies.length;i++){
    	   if(cookies[i].getName().startsWith("GOODS_")){
    		   sum++;
    	   }
       }
       //小于5,可直接加入cookie
       if(sum<5 &&sum>=0){
		   //检查在已经存在的cookie里面是否已经有了该cookie
		   int j=0;
           for(int i=0;i<cookies.length;i++){
        	   if(cookies[i].getName().endsWith(name) &&cookies[i].getName().startsWith("GOODS_")){

        		   //已经存在则删除
        		   cookies[i].setMaxAge(0);
        		   response.addCookie(cookies[i]);
        		   //再新填入cookies中
        		   Cookie cookie=new Cookie("GOODS_"+name,name);
        		   response.addCookie(cookie);
        	   }else{
        		   j++;
        	   }
           }
    	   //如果在已经存在的cookie中没有保存该cookie,则直接加入
    	   if(j==cookies.length){
        	   Cookie cookie=new Cookie("GOODS_"+name,name);
        	   response.addCookie(cookie);
    	   }

       }
     if(sum>=5){
    	   //如果cookie>=5,则需要删除
    			   //检查在已经存在的5个cookie里面是否已经有了该cookie
    			   int j=0;
           for(int i=0;i<cookies.length;i++){
        	   if(cookies[i].getName().endsWith(name)&&cookies[i].getName().startsWith("GOODS_")){
        		   //已经存在则删除
        		   cookies[i].setMaxAge(0);
        		   response.addCookie(cookies[i]);
        		   //再新填入cookies中
        		   Cookie cookie=new Cookie("GOODS_"+name,name);
        		   response.addCookie(cookie);
        		   
        	   }else{
        		   j++;
        	   }
           }
           if(j==cookies.length){
        	   //如果在已经存在的5个cookie中没有保存该cookie,则删除第一个cookie,然后再保存
               for(int k=0;k<cookies.length;k++){
            	   if(cookies[k].getName().startsWith("GOODS_")){
            		   cookies[k].setMaxAge(0);
            		   response.addCookie(cookies[k]);
            		   break;
            	   }
            	   
               }  		   
       		   //再新填入cookies中
       		   Cookie cookie=new Cookie("GOODS_"+name,name);
       		   response.addCookie(cookie);  
           }
 
       }
    %>
    <a href="<%=basePath%>goods/list.jsp">return</a>
  </body>
</html>

测试:

   1)进入list.jsp:

  2)点击A商品,进入A的详情页detail.jsp:

  3)点击return返回list.jsp:

4)点击B进入B的详情页:

5)点击return返回list.jsp:

6)依次点击C D E然后再返回:

这时已经达到5个浏览记录,如果再继续浏览,会有以前的cookie被删除,新的cookie被加入:

 7)。点击A:

8)。点击D:

9).点击I:



  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值