Cookie common operations class in Java (Action cookie in spring)

Source: Internet
Author: User

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:

    1. The parameter name of the value request argument;
    2. Required the parameter is required, the default is true (required), when set to be required, if there is no incoming parameters, error;
    3. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.