Java Setting cookie Value

Source: Internet
Author: User

Original address: http://blog.csdn.net/lansetiankong12/article/details/52622654

/**

* Read All Cookies

* Note: When reading cookies from the client, other properties, including MaxAge, are unreadable and will not be submitted. When a browser submits a cookie, it submits only the name and Value property. The MaxAge property is used only by the browser to determine whether the cookie expires

* @param request

* @param response

*/

        

@RequestMapping ("/showcookies")          publicvoid showcookies (httpservletrequest request,httpservletresponse Response) {              = request.getcookies ();   This allows you to get a cookie  array            if(null= =cookie) {                  System.out.println ( "no cookie=========");              } Else {                  for(Cookie cookie:cookies) {                      System.out.println ("Name:" + Cookie.getname () + ", Value:" + Cookie.getvalue ());   }}}

/**

* Add a cookie

* @param response

* @param name

* @param value

*/

@RequestMapping ("/addcookie")          publicvoid addcookie (httpservletresponse response,string name,string value) {               = Newcookie (Name.trim (), Value.trim ());              Cookie.setmaxage (30* 60); // set to 30min               Cookie.setpath ("/");              System.out.println ("added ===============");              Response.addcookie (cookie);          }  

/**

* Modify Cookies

* @param request

* @param response

* @param name

* @param value

* Notice that when you modify or delete a cookie, all properties except value, MaxAge, such as name, path, domain, and so on, are identical to the original cookie. Otherwise, the browser will be treated as two different cookies without overwriting, causing the modification and deletion to fail .

*/

@RequestMapping ("/editcookie") publicvoid Editcookie (httpservletrequest request,httpservletresponse response,string name,String value) { Cookie[] Cookies=request.getcookies (); if(NULL==cookies) {System.out.println ("No cookie=============="); }Else{                   for(Cookie cookie:cookies) {if(Cookie.getname (). Equals (name)) {System.out.println ("The original value is:" +Cookie.getvalue ());                          Cookie.setvalue (value); Cookie.setpath ("/"); Cookie.setmaxage (30* 60);//set to 30minSystem.out.println (The modified cookie is named: "+cookie.getname () +" and the new value is: "+Cookie.getvalue ());                          Response.addcookie (cookie);  Break; }                  }              }          }  

/**

* Delete Cookies

* @param request

* @param response

* @param name

*/

@RequestMapping ("/delcookie") publicvoid Delcookie (httpservletrequest request,httpservletresponse response,string name) {Coo Kie[] Cookies=request.getcookies (); if(NULL==cookies) {System.out.println ("No cookie=============="); }Else{                   for(Cookie cookie:cookies) {if(Cookie.getname (). Equals (name)) {Cookie.setvalue (NULL); Cookie.setmaxage (0);//immediately destroy CookiesCookie.setpath ("/"); System.out.println ("Deleted cookie Name:" +cookie.getname ());                          Response.addcookie (cookie);  Break; }                  }              }          }  

/**

* Get cookies by name

* @param request

* @param name Cookie name

* @return

*/

   Public Cookie getcookiebyname (httpservletrequest request,string name) {          Map<String,Cookie> cookiemap = Readcookiemap (request);           if (Cookiemap.containskey (name)) {              = (Cookie) cookiemap.get (name);              Returncookie;          } Else {              returnnull;          }        }  

It is usually developed with the following code to encapsulate the acquired cookie

/**

* Encapsulate cookies in Map

* @param request

* @return

*/

Private Map<string,cookie> Readcookiemap (httpservletrequest request) {            Map<String,Cookie> Cookiemap = newhashmap<string,cookie>();           = request.getcookies ();           if (null! =cookies)              {for(Cookie cookie:cookies) {                  cookiemap.put ( Cookie.getname (), cookie);              }          }          Returncookiemap;      }  

Java Setting cookie Value

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.