Cookie Object Toolkit, object Add, get, modify-Pro test available

Source: Internet
Author: User

Let's start by understanding the concepts of cookies and session objects.

First, the cookie is the client-side caching technology, the size is generally about 4KB, mainly store some relatively small information, commonly used examples have a user name and password, and is not secure;

Session is the server cache technology, size does not control, data information stored in the server, is safe.

Here is a cookie toolkit that I have repeatedly tested and encapsulated for learning to use.

1  Packagecom.rick.framework.utils;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 6 ImportJavax.servlet.http.Cookie;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten /** One * Cookiesutil.java A  * @authorRick - * 2016 May 2, 2016 PM 8:59:00 - * Name&path: It is recommended to use "constant" definition the  */ -  Public classCookiesutil { -      -     /** + * Add Cookie Object -      * @paramResponse Request +      * @paramname names A      * @paramValue Content at      * @paramPath paths -      * @paramexpiry effective time (seconds) -      * @return -      */ -      Public Static BooleanSetcookie (httpservletresponse response, string name, string value, String path,intexpiry) { -Cookie cookie =NewCookie (name, value); inCookie.setmaxage (expiry);//Cookie Effective Time (seconds):-1 Close browser fails, 0 immediately expires, greater than 1 for specific time - Cookie.setpath (path); to Response.addcookie (cookie); +  -         return false; the     } *      $     Panax Notoginseng     /** - * Get cookies by name the      * @paramRequest +      * @paramname names A      * @paramPath paths the      * @return +      */ -      Public StaticCookie Getcookiebyname (httpservletrequest request, String name,string path) { $map<string, cookie> cookiemap =Readcookiemap (request); $         if(Cookiemap.containskey (name)) { -Cookie cookie =(Cookie) cookiemap.get (name); -             returncookies; the}Else { -             return NULL;Wuyi         } the     } -  Wu     /** - * Remove Client Cookie Object About      * @paramRequest Requests $      * @paramResponse Response -      * @paramname names -      * @paramPath paths -      * @return A      */ +      Public Static BooleanRemovecookie (httpservletrequest request,httpservletresponse response,string name,string path) { the         //--Clear Client cookie -         Try { $Cookie cookie =cookiesutil.getcookiebyname (Request, name, path); the             if(cookie!=NULL){ theCookie.setmaxage (0); the Cookie.setpath (path); theCookie.setvalue (NULL); - Response.addcookie (cookie); in                 return true; the             } the}Catch(Exception e) {} About         return false; the     } the      the     /** + * Encapsulate cookies in map -      *  the      * @paramRequestBayi      * @return the      */ the     Private StaticMap<string, cookie>Readcookiemap (HttpServletRequest request) { -map<string, cookie> cookiemap =NewHashmap<string, cookie>(); -cookie[] Cookies =request.getcookies (); the         if(NULL!=cookies) { the              for(Cookie cookie:cookies) { the Cookiemap.put (Cookie.getname (), cookie); the             } -         } the         returnCookiemap; the     } the     94}

"Sign In"-Set cookies

Cookiesutil.setcookie (response, Iconstant.user_cookie_name, User.getname (), Iconstant.user_cookie_path, 60*10);//- -Set the user's 10-minute effective time

"Personal Center"-Get Cookies

Cookie cookie = cookiesutil.getcookiebyname (request, Iconstant.user_cookie_name,iconstant.user_cookie_path);//-- Get cookie object using name + path

"Exit"-Clear cookies

Cookiesutil.removecookie (Request, Response, Iconstant.user_cookie_name, Iconstant.user_cookie_path);//--by name + Path Delete Cookie Object

Cookie Object Toolkit, object Add, get, modify-Pro test available

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.