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

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

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放最前面)
上一篇:eclipse导入外部项目引发的错误以及解决方案


下一篇:6.3 file 常用方法