javaWeb 使用cookie显示商品浏览记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaWeb 使用cookie显示商品浏览记录相关的知识,希望对你有一定的参考价值。

package de.bvb.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

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

/**
 * 显示商品列表和已经浏览过的商品
 * 
 * @author joker
 * 
 */
public class CookieDemo11 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        // 显示商品列表
        out.write("商品列表: <br/>");
        for (Entry<String, Book> b : Db.getBooks().entrySet()) {
            out.write("<a target=‘_blank‘ href=‘/web/CookieDemo12?id="
                    + b.getKey() + "‘>" + b.getValue().getName() + "</a><br/>");
        }

        // 显示浏览过的商品
        out.write(" <br/> <br/>浏览过的商品: <br/>");
        Cookie[] cookies = request.getCookies();
        for (int i = 0; cookies != null && i < cookies.length; i++) {
            if (cookies[i].getName().equals("history")) {
                String[] ids = cookies[i].getValue().split("\\,");
                for (String s : ids) {
                    out.write(Db.getBooks().get(s).getName() + "<br/>");
                }
            }
        }
    }

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

        doGet(request, response);
    }
}

class Db {
    private static LinkedHashMap<String, Book> books = new LinkedHashMap<String, Book>();
    static {
        books.put("1", new Book("1", "javaWeb", "javaweb"));
        books.put("2", new Book("2", "android", "android"));
        books.put("3", new Book("3", "spring", "spring"));
        books.put("4", new Book("4", "strus", "strus"));
        books.put("5", new Book("5", "ios", "ios"));
    }

    public static Map<String, Book> getBooks() {
        return books;
    }
}

class Book {
    private String id;
    private String name;
    private String description;

    public Book(String id, String name, String description) {
        super();
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public Book() {
        super();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

 

 

package de.bvb.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.Map.Entry;

import javax.enterprise.inject.ResolutionException;
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 javax.swing.text.DateFormatter;

/**
 * 显示商品详情
 * 
 * @author joker
 * 
 */
public class CookieDemo12 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();

        // 显示商品详情
        out.write("商品详情: <br/>");
        String id = request.getParameter("id");
        for (Entry<String, Book> b : Db.getBooks().entrySet()) {
            if (id.equals(b.getKey())) {
                out.write(b.getValue().getId() + "<br/>");
                out.write(b.getValue().getName() + "<br/>");
                out.write(b.getValue().getDescription() + "<br/>");
            }
        }

        // 回写cookie,保存最后浏览的商品id
        String history = buildIds(id, request);
        Cookie cookie = new Cookie("history", history);
        cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
        cookie.setPath("/web");
        response.addCookie(cookie);
    }

    private String buildIds(String id, HttpServletRequest request) {
        String history = null;
        Cookie[] cookies = request.getCookies();
        for (int i = 0; cookies != null && i < cookies.length; i++) {
            if (cookies[i].getName().equals("history")) {
                history = cookies[i].getValue();
            }
        }
        if (history == null) {
            return id;
        }
        LinkedList<String> ids = new LinkedList<String>(Arrays.asList(history
                .split("\\,")));
        if (ids.contains(id)) {
            ids.remove(id);
        } else {
            if (ids.size() >= 3) { // 最多显示3条浏览历史
                ids.removeLast();
            }
        }
        ids.addFirst(id);
        StringBuffer sb = new StringBuffer();
        for (String s : ids) {
            sb.append(s).append(",");
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }

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

        doGet(request, response);
    }

}

 

以上是关于javaWeb 使用cookie显示商品浏览记录的主要内容,如果未能解决你的问题,请参考以下文章

显示商品的浏览记录

Cookie在商品浏览记录中的使用

JavaWeb学习笔记五 会话技术Cookie&Session

用JS中的cookie实现商品的浏览记录

Servlet—Cookie(显示用户上次访问时间显示商品浏览历史)

(JS实现顾客商品浏览记录以及购物车)Cookie的保存与删除