Cookie工具类
/** * Copyright © 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. */ package com.thinkgem.jeesite.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.ui.Model; /** * Cookie工具类 * @author ThinkGem * @version 2013-01-15 */ public class CookieUtils { /** * 设置 Cookie(生成时间为1天) * @param name 名称 * @param value 值 */ public static void setCookie(HttpServletResponse response, String name, String value) { setCookie(response, name, value, 60*60*24); } /** * 设置 Cookie * @param name 名称 * @param value 值 * @param maxAge 生存时间(单位秒) * @param uri 路径 */ public static void setCookie(HttpServletResponse response, String name, String value, String path) { setCookie(response, name, value, path, 60*60*24); } /** * 设置 Cookie * @param name 名称 * @param value 值 * @param maxAge 生存时间(单位秒) * @param uri 路径 */ public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) { setCookie(response, name, value, "/", maxAge); } /** * 设置 Cookie * @param name 名称 * @param value 值 * @param maxAge 生存时间(单位秒) * @param uri 路径 */ public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) { Cookie cookie = new Cookie(name, null); cookie.setPath(path); cookie.setMaxAge(maxAge); try { cookie.setValue(URLEncoder.encode(value, "utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.addCookie(cookie); } /** * 获得指定Cookie的值 * @param name 名称 * @return 值 */ public static String getCookie(HttpServletRequest request, String name) { return getCookie(request, null, name, false); } /** * 获得指定Cookie的值,并删除。 * @param name 名称 * @return 值 */ public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) { return getCookie(request, response, name, true); } /** * 获得指定Cookie的值 * @param request 请求对象 * @param response 响应对象 * @param name 名字 * @param isRemove 是否移除 * @return 值 */ public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean isRemove) { String value = null; Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) { try { value = URLDecoder.decode(cookie.getValue(), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (isRemove) { cookie.setMaxAge(0); response.addCookie(cookie); } } } } return value; } /** * 生成cookie的值 浏览过商品id的拼接 * @return 生成cookie的id拼接值 */ public static String createValue(HttpServletRequest request,String id) { String exhiHist = CookieUtils.getCookie(request, "exhiHist"); // null或没有exhiHist if( StringUtils.isBlank(exhiHist)){ //直接返回传入的id return id; } // 3,2,1 2 //String -> String[] -> Collection :为了方便判断重复id String[] ids = exhiHist.split(","); Collection colls = Arrays.asList(ids); //<3,21> // LinkedList 方便地操作(增删改元素)集合 // Collection -> LinkedList LinkedList list = new LinkedList(colls); //不超过4个 if(list.size()<6){ //id重复 if(list.contains(id)){ //去除重复id,把传入的id放最前面 list.remove(id); list.addFirst(id); }else{ //直接把传入的id放最前面 list.addFirst(id); } }else{ //等于4个 //id重复 if(list.contains(id)){ //去除重复id,把传入的id放最前面 list.remove(id); list.addFirst(id); }else{ //去最后的id,把传入的id放最前面 list.removeLast(); list.addFirst(id); } } // LinedList -> String StringBuffer sb = new StringBuffer(); for (Object object : list) { sb.append(object+","); } //去掉最后的逗号 String result = sb.toString(); result = result.substring(0, result.length()-1); return result; } /** * 获取我的足迹的方法 * @param model * @param request */ public static void getMyWatch(Model model, HttpServletRequest request,HttpServletResponse response) { try { String exhiIds = CookieUtils.getCookie(request, "exhiHist"); if(StringUtils.isNotBlank(exhiIds)){ String[] obj = exhiIds.split(","); List<Map<String, String>> list = new ArrayList<Map<String,String>>(); for (String product : obj) { String[] elem = product.split(":"); if(elem.length>0){ Map<String, String> p = new HashMap<String,String>(); p.put("total", elem[0]); p.put("zhanWeiArea", elem[1]); p.put("tiMu", elem[2]); p.put("url", elem[3]); list.add(p); } } model.addAttribute("hist", list); } } catch (Exception e) { CookieUtils.setCookie(response, "exhiHist", ""); } } /** * 获取Cookie 值 * @param request * @return */ public static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){ Map<String,Cookie> cookieMap = new HashMap<String,Cookie>(); Cookie[] cookies = request.getCookies(); if(null!=cookies){ for(Cookie cookie : cookies){ cookieMap.put(cookie.getName(), cookie); } } return cookieMap; } /** * * @param request * @param response * @param name * @return */ public static boolean clearCookie(HttpServletRequest request,HttpServletResponse response, String name){ boolean bool = false; Cookie[] cookies = request.getCookies(); try { for(int i =0;i<cookies.length;i++){ Cookie cookie = new Cookie(name, null); cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); bool=true; } } catch (Exception e) { e.printStackTrace(); } return bool; } }