标签:常用 快速 key UI log logs 属性 null util
说明:Cookie下用Key取值没有快速的方法,只能便利循环去取。
import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CookieTool { /** * 添加cookie * * @param response * @param name Key * @param value Value * @param maxAge 有效时间 */ public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { Cookie cookie = new Cookie(name, value); cookie.setPath("/"); cookie.setMaxAge(maxAge); cookie.setDomain(".jsoft.me"); // cookie作用域 response.addCookie(cookie); } /** * 检索所有Cookie封装到Map集合 * * @param request * @return */ public static Map<String, String> readCookieMap(HttpServletRequest request) { Map<String, String> cookieMap = new HashMap<String, String>(); Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { cookieMap.put(cookie.getName(), cookie.getValue()); } } return cookieMap; } /** * 通过Key获取Value * * @param request * @param name Key * @return Value */ public static String getCookieValueByName(HttpServletRequest request, String name) { Map<String, String> cookieMap = readCookieMap(request); if (cookieMap.containsKey(name)) { String cookieValue = (String) cookieMap.get(name); return cookieValue; } else { return null; } } }
在Spring MVC的Controller中使用:
在Spring MVC中使用CookieValue注解映射请求参数,@CookieValue有三个属性,分别如下:
参考:
http://www.cnblogs.com/jun-ma/p/5679459.html
http://blog.csdn.net/u011848397/article/details/52201339
http://www.ibloger.net/article/33.html(以上内容转自此篇文章)
Java中Cookie常用操作类(Spring中操作Cookie)
标签:常用 快速 key UI log logs 属性 null util
原文地址:http://www.cnblogs.com/EasonJim/p/7721950.html