查看用户浏览器过的商品--Cookie

简介:

JERFW_AUDC4S23Y9_PDDM_S

ProductDao:list数组模拟数据库,获取数据
Product:model 把数据对象化
Logic:service层
DetailServlet:显示单个商品数据
ListServlet:商品集合显示和显示浏览过的商品。

ProductDao:

package dao;

import java.util.ArrayList;
import java.util.List;
import entity.*;
public class ProductDao {
    static List<Product> list=new ArrayList<Product>();
    static {
        for(int i=0;i<10;i++) {
            
            Product product=new Product();
            product.setNumber(i);
            product.setName("我是"+i);
            list.add(product);
        }
    }
    
    public Product findOne(int id) {
        for (Product product:list) {
            if(product.getNumber()==id) {
                return product;
            }
        }
        return null;
    }
    
    public List<Product> findList(){
        return list;
    }
}

Product:

package entity;

public class Product {
    private int number;
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private String name;
    
    
    
}

Logic:

package service;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import dao.ProductDao;
import entity.Product;
public class Logic {
    public Product findOne(int id) {
        ProductDao productDao=new ProductDao();
        return productDao.findOne(id);
    }
    public List<Product> findList(){
        ProductDao productDao=new ProductDao();
        return productDao.findList();
    }
}

DetailServlet:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Product;
import javafx.print.Collation;
import service.*;
@WebServlet("/DetailServlet")
public class DetailServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        response.setContentType("text/html;charset=utf-8");
        int id=Integer.parseInt(request.getParameter("id"));
        Logic logic=new Logic();
        Product product=logic.findOne(id);
        
        String name=product.getName();
        PrintWriter printWriter=response.getWriter();
        String html="";
        html+="<html>";
        html+="<body>";
        html+="<table border='1' align='center' width='600px'>";
        html+="<tr>";
        html+="<th>编号</th><th>名字</th>";
        html+="</tr>";
        if(product!=null) {
            html+="<tr>";
            html+="<td>"+id+"</td>"+"<td>"+name+"</td>";
            html+="</tr>";    
        }
        html+="</table>";
        html+="<center><a href='"+request.getContextPath()+"/ListServlet'>"+"返回列表</a></center>";
        html+="</body>";
        html+="</html>";
        printWriter.write(html);
        Cookie cookie=new Cookie("lastone", createValue(request, id));
        cookie.setMaxAge(100);
        response.addCookie(cookie);
    }

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

        doGet(request, response);
    }
    //修改接收到的Cookie值
    public String createValue(HttpServletRequest request,int id) {
        String idString=String.valueOf(id);
        String prodHist=null;
        Cookie[] cookies=request.getCookies();
        if(cookies!=null) {
            for(Cookie cookie:cookies) {
                if(cookie.getName().equals("lastone")) {
                    prodHist=cookie.getValue();
                    break;
                }
            }
        }
            if(prodHist==null||cookies==null) {
                return idString;
            }
            String[] strings=prodHist.split("#");
            List<String> collation=Arrays.asList(strings);
            LinkedList<String> linkedList=new LinkedList<String>(collation);
            
            if(linkedList.size()<3) {
                if(linkedList.contains(idString)) {
                    linkedList.remove(idString);
                    linkedList.addFirst(idString);
                }else {
                    linkedList.addFirst(idString);
                }
            }else {
                if(linkedList.contains(idString)) {
                    linkedList.remove(idString);
                    linkedList.addFirst(idString);
                }else {
                    linkedList.removeLast();
                    linkedList.addFirst(idString);
                }
                
            }
            StringBuffer stringBuffer=new StringBuffer();
            for(String string:linkedList) {
                stringBuffer.append(string+"#");
            }
            String result=stringBuffer.toString();
            result=result.substring(0, result.length()-1);
            return result;

    }
}

ListServlet:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Product;
import service.Logic;

@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        response.setContentType("text/html;charset=utf-8");
        //service层
        Logic logic=new Logic();
        List<Product> list=logic.findList();
        PrintWriter printWriter=response.getWriter();
        String html="";
        html+="<html>";
        html+="<body>";
        html+="<table border='1' align='center' width='600px'>";
        html+="<tr>";
        html+="<th>编号</th><th>名字</th>";
        html+="</tr>";
        for(Product One:list) {
            String name=One.getName();
            int id=One.getNumber();
            html+="<tr>";
            html+="<td>"+id+"</td>"+"<td><a href='"+request.getContextPath()+"/DetailServlet?id="+id+"'>"+name+"</a></td>";
            html+="</tr>";    
        }
        html+="</table>";
        
        String value=null;
        Cookie[] cookies= request.getCookies();
        if(cookies!=null) {
            for(Cookie cookie:cookies) {
                if(cookie.getName().equals("lastone")) {
                    value=cookie.getValue();
                    break;
                }
            }
            String[] strings=value.split("#");
            for(String number:strings) {
                Product product=logic.findOne(Integer.parseInt(number));
                html+=product.getNumber()+"   "+product.getName()+"<br/>";
            }
        }
        html+="</body>";
        html+="</html>";
        printWriter.write(html);
    }

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

        doGet(request, response);
    }

}

难点:如何保留浏览过的商品?

生成cookie的值
分析:
当前cookie值           传入商品id      最终cookie值
null或没有prodHist          1             1    (算法: 直接返回传入的id )
        1                  2            2,1 (没有重复且小于3个。算法:直接把传入的id放最前面 )
         2,1               1            1,2(有重复且小于3个。算法:去除重复id,把传入的id放最前面 )
         3,2,1             2            2,3,1(有重复且3个。算法:去除重复id,把传入的id放最前面)
         3,2,1             4            4,3,2(没有重复且3个。算法:去最后的id,把传入的id放最前面)
目录
相关文章
|
存储 搜索推荐 数据挖掘
使用selenium库模拟浏览器行为,获取网页的cookie值
使用selenium库模拟浏览器行为,获取网页的cookie值
|
12天前
|
存储 缓存 网络协议
计算机网络常见面试题(二):浏览器中输入URL返回页面过程、HTTP协议特点,GET、POST的区别,Cookie与Session
计算机网络常见面试题(二):浏览器中输入URL返回页面过程、HTTP协议特点、状态码、报文格式,GET、POST的区别,DNS的解析过程、数字证书、Cookie与Session,对称加密和非对称加密
|
6月前
|
存储 搜索推荐 安全
Cookie 探秘:了解 Web 浏览器中的小甜饼
Cookie 探秘:了解 Web 浏览器中的小甜饼
|
2月前
|
存储 编解码 JSON
解决浏览器存储问题,不得不了解的cookie、localStorage和sessionStorage
该文章详细对比了浏览器存储机制中的cookie、localStorage和sessionStorage的不同之处,以及各自的适用场景。
|
3月前
|
存储
【Azure APIM】APIM 策略语句如何来设置多个Cookie值让浏览器保存
【Azure APIM】APIM 策略语句如何来设置多个Cookie值让浏览器保存
|
4月前
|
存储 Web App开发 JavaScript
浏览器【详解】Cookie(含Cookie的起源,属性,个数和大小限制,作用,优点,缺点,JS 的操作方法等)
浏览器【详解】Cookie(含Cookie的起源,属性,个数和大小限制,作用,优点,缺点,JS 的操作方法等)
207 0
|
6月前
|
存储 缓存
浏览器缓存sessionStorage、localStorage、Cookie
浏览器缓存sessionStorage、localStorage、Cookie
77 1
|
6月前
|
Web App开发
允许浏览器cookie策略处理
允许浏览器cookie策略处理
50 0
|
Web App开发
【cookie处理】79chrome浏览器 -允许浏览器cookie策略处理
【cookie处理】79chrome浏览器 -允许浏览器cookie策略处理
|
12月前
|
存储 缓存
浏览器缓存sessionStorage、localStorage、Cookie
浏览器缓存sessionStorage、localStorage、Cookie
144 1