Java implementation of cookie additions and deletions

Source: Internet
Author: User

Original: http://blog.csdn.net/k21325/article/details/54377830

1.springMVC Frame:
         [Java]View PlainCopy
  1. /**
  2. * Read All Cookies
  3. * 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
  4. * @param request
  5. * @param response
  6. */
  7. @RequestMapping ("/showcookies")
  8. public void Showcookies (httpservletrequest request,httpservletresponse response) {
  9. cookie[] cookies = request.getcookies (); //This allows you to get a cookie array
  10. if (null==cookies) {
  11. System.out.println ("no cookie=========");
  12. } Else {
  13. For (Cookie cookie:cookies) {
  14. System.out.println ("Name:" +cookie.getname () +", Value:" + cookie.getvalue ());
  15. }
  16. }
  17. }
         [Java]View PlainCopy
  1. /**
  2. * Add a cookie
  3. * @param response
  4. * @param name
  5. * @param value
  6. */
  7. @RequestMapping ("/addcookie")
  8. public void Addcookie (HttpServletResponse response,string name,string value) {
  9. Cookie cookie = New Cookie (Name.trim (), Value.trim ());
  10. Cookie.setmaxage (* ); Set to 30min
  11. Cookie.setpath ("/");
  12. System.out.println ("added ===============");
  13. Response.addcookie (cookie);
  14. }
       [Java]View PlainCopy
  1. /**
  2. * Modify Cookies
  3. * @param request
  4. * @param response
  5. * @param name
  6. * @param value
  7. * 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.
  8. */
  9. @RequestMapping ("/editcookie")
  10. public void Editcookie (httpservletrequest request,httpservletresponse response,string name,string value) {
  11. cookie[] cookies = request.getcookies ();
  12. if (null==cookies) {
  13. System.out.println ("no cookie==============");
  14. } Else {
  15. For (Cookie cookie:cookies) {
  16. if (Cookie.getname (). Equals (name)) {
  17. System.out.println ("original value:" +cookie.getvalue ());
  18. Cookie.setvalue (value);
  19. Cookie.setpath ("/");
  20. Cookie.setmaxage (* ); Set to 30min
  21. System.out.println ("The modified cookie name is:" +cookie.getname () +", the new value is:" +cookie.getvalue ());
  22. Response.addcookie (cookie);
  23. Break ;
  24. }
  25. }
  26. }
  27. }
        [Java]View PlainCopy
  1. /**
  2. * Delete Cookies
  3. * @param request
  4. * @param response
  5. * @param name
  6. */
  7. @RequestMapping ("/delcookie")
  8. public void Delcookie (httpservletrequest request,httpservletresponse response,string name) {
  9. cookie[] cookies = request.getcookies ();
  10. if (null==cookies) {
  11. System.out.println ("no cookie==============");
  12. } Else {
  13. For (Cookie cookie:cookies) {
  14. if (Cookie.getname (). Equals (name)) {
  15. Cookie.setvalue (null);
  16. Cookie.setmaxage (0); Immediately destroy Cookies
  17. Cookie.setpath ("/");
  18. System.out.println ("deleted cookie Name:" +cookie.getname ());
  19. Response.addcookie (cookie);
  20. Break ;
  21. }
  22. }
  23. }
  24. }

2. In general, cookies are encapsulated first:
[Java]View PlainCopy
  1. /**
  2. * Get cookies by name
  3. * @param request
  4. * @param name Cookie name
  5. * @return
  6. */
  7. Public Cookie getcookiebyname (httpservletrequest request,string name) {
  8. map<string,cookie> Cookiemap = readcookiemap (request);
  9. if (Cookiemap.containskey (name)) {
  10. Cookie cookie = (cookie) cookiemap.get (name);
  11. return cookie;
  12. }else{
  13. return null;
  14. }
  15. }
[Java]View PlainCopy
  1. /**
  2. * Encapsulate cookies in Map
  3. * @param request
  4. * @return
  5. */
  6. private map<string,cookie> Readcookiemap (HttpServletRequest request) {
  7. map<string,cookie> Cookiemap = new hashmap<string,cookie> ();
  8. cookie[] cookies = request.getcookies ();
  9. if (null!=cookies) {
  10. For (Cookie cookie:cookies) {
  11. Cookiemap.put (Cookie.getname (), cookie);
  12. }
  13. }
  14. return cookiemap;
  15. }


Java implementation of cookie additions and deletions

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.