查看用户浏览器过的商品--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放最前面)
目录
相关文章
|
7月前
|
存储 搜索推荐 数据挖掘
使用selenium库模拟浏览器行为,获取网页的cookie值
使用selenium库模拟浏览器行为,获取网页的cookie值
|
4月前
|
存储 JavaScript PHP
什么是cookie,如何设置在浏览器页面关闭后清除cookie
什么是cookie,如何设置在浏览器页面关闭后清除cookie
93 0
|
9月前
|
Web App开发
【cookie处理】79chrome浏览器 -允许浏览器cookie策略处理
【cookie处理】79chrome浏览器 -允许浏览器cookie策略处理
|
4月前
|
Web App开发
浏览器导入和导出cookie
浏览器导入和导出cookie
|
9月前
|
存储 Web App开发 缓存
前端(二):浏览器本地存储 - cookie、localStorage、sessionStorage
浏览器本地存储 - cookie、localStorage、sessionStorage
100 0
|
9月前
|
存储 安全 前端开发
浏览器:理解HTTP无状态与Cookie的使用
浏览器:理解HTTP无状态与Cookie的使用
88 0
|
JSON JavaScript 数据格式
js-cookie读写浏览器中的Cookie
js-cookie读写浏览器中的Cookie
149 0
|
JSON Java 数据格式
浏览器Header和cookie字符串形式转Json
浏览器Header和cookie字符串形式转Json
106 0
浏览器Header和cookie字符串形式转Json
|
存储 Web App开发 编解码
浏览器原理 32 # 跨站脚本攻击(XSS):为什么Cookie中有HttpOnly属性?
浏览器原理 32 # 跨站脚本攻击(XSS):为什么Cookie中有HttpOnly属性?
100 0
浏览器原理 32 # 跨站脚本攻击(XSS):为什么Cookie中有HttpOnly属性?
|
前端开发 JavaScript CDN
js-cookie读写浏览器中的Cookie及其应用
js-cookie读写浏览器中的Cookie及其应用
177 0