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("商品列表: 
"); for (Entry b : Db.getBooks().entrySet()) { out.write("" + b.getValue().getName() + "
"); } // 显示浏览过的商品 out.write("

浏览过的商品:
"); 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() + "
"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } class Db { private static LinkedHashMap books = new LinkedHashMap(); 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 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("商品详情: 
"); String id = request.getParameter("id"); for (Entry b : Db.getBooks().entrySet()) { if (id.equals(b.getKey())) { out.write(b.getValue().getId() + "
"); out.write(b.getValue().getName() + "
"); out.write(b.getValue().getDescription() + "
"); } } // 回写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 ids = new LinkedList(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); } }

 

转载于:https://www.cnblogs.com/Westfalen/p/5962866.html

你可能感兴趣的:(javaWeb 使用cookie显示商品浏览记录)