Note: There is no quick way to use key under a cookie, only to facilitate the loop to fetch.
ImportJava.util.HashMap;ImportJava.util.Map;ImportJavax.servlet.http.Cookie;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classCookietool {/*** Add a cookie * *@paramResponse *@paramname Key *@paramValue Value *@paramMaxAge Effective Time*/ Public Static voidAddcookie (httpservletresponse response, string name, String value,intMaxAge) {Cookie Cookie=NewCookie (name, value); Cookie.setpath ("/"); Cookie.setmaxage (MaxAge); Cookie.setdomain (". Jsoft.me");//Cookie ScopeResponse.addcookie (cookie); } /*** Retrieve all cookies encapsulated into the map collection * *@paramRequest *@return */ Public StaticMap<string, string>Readcookiemap (HttpServletRequest request) {Map<string, string> cookiemap =NewHashmap<string, string>(); Cookie[] Cookies=request.getcookies (); if(NULL!=cookies) { for(Cookie cookie:cookies) {cookiemap.put (Cookie.getname (), Cookie.getvalue ()); } } returnCookiemap; } /*** Get value by key * *@paramRequest *@paramname Key *@returnValue*/ Public Staticstring Getcookievaluebyname (HttpServletRequest request, string name) {Map<string, string> cookiemap =Readcookiemap (Request); if(Cookiemap.containskey (name)) {String Cookievalue=(String) cookiemap.get (name); returnCookievalue; } Else { return NULL; } }}
Used in the controller of Spring MVC:
Using the Cookievalue annotation map request parameter in spring MVC, @CookieValue has three properties, as follows:
- The parameter name of the value request argument;
- Required the parameter is required, the default is true (required), when set to be required, if there is no incoming parameters, error;
- DefaultValue set the default value of the request parameter;
Reference:
Http://www.cnblogs.com/jun-ma/p/5679459.html
http://blog.csdn.net/u011848397/article/details/52201339
Http://www.ibloger.net/article/33.html (the above content is transferred from this article)
Cookie common operations class in Java (Action cookie in spring)