Cookie Tool Class

Source: Internet
Author: User

1 ImportJavax.servlet.http.Cookie;2 Importjavax.servlet.http.HttpServletRequest;3 ImportJavax.servlet.http.HttpServletResponse;4 Importjava.io.UnsupportedEncodingException;5 ImportJava.net.URLDecoder;6 ImportJava.net.URLEncoder;7 8 /**9 * Cookie Tool classTen  */ One  Public classCookieutil { A     /** - * Set Cookies (Generate time is 1 days) -      * @paramname names the      * @paramValue Values -      */ -      Public Static voidSetcookie (httpservletresponse response, string name, String value) { -Setcookie (response, name, value, 60*60*24); +     } -  +     /** A * Set Cookies at      * @paramname names -      * @paramValue Values -      */ -      Public Static voidSetcookie (httpservletresponse response, string name, string value, String path) { -Setcookie (response, name, value, path, 60*60*24); -     } in  -     /** to * Set Cookies +      * @paramname names -      * @paramValue Values the      * @paramMaxAge Survival time (in seconds) *      */ $      Public Static voidSetcookie (httpservletresponse response, string name, String value,intMaxAge) {Panax NotoginsengSetcookie (response, name, value, "/", maxAge); -     } the  +     /** A * Set Cookies the      * @paramname names +      * @paramValue Values -      * @paramMaxAge Survival time (in seconds) $      */ $      Public Static voidSetcookie (httpservletresponse response, string name, string value, String path,intMaxAge) { -Cookie cookie =NewCookie (Name,NULL); - Cookie.setpath (path); the cookie.setmaxage (maxAge); -         Try {WuyiCookie.setvalue (Urlencoder.encode (Value, "Utf-8")); the}Catch(unsupportedencodingexception e) { - e.printstacktrace (); Wu         } - Response.addcookie (cookie); About     } $  -     /** - * Get the value of the specified cookie -      * @paramname names A      * @returnvalue +      */ the      Public Staticstring GetCookie (HttpServletRequest request, string name) { -         returnGetCookie (Request,NULL, Name,false); $     } the     /** the * Get the value of the specified cookie and delete it.  the      * @paramname names the      * @returnvalue -      */ in      Public Staticstring GetCookie (httpservletrequest request, httpservletresponse Response, string name) { the         returnGetCookie (Request, response, name,true); the     } About     /** the * Get the value of the specified cookie the      * @paramRequest Object the      * @paramResponse Response Object +      * @paramName names -      * @paramIsremove whether to remove the      * @returnvalueBayi      */ the      Public StaticString GetCookie (HttpServletRequest request, httpservletresponse Response, string name,Booleanisremove) { theString value =NULL; -cookie[] Cookies =request.getcookies (); -         if(Cookies! =NULL) { the              for(Cookie cookie:cookies) { the                 if(Cookie.getname (). Equals (name)) { the                     Try { theValue = Urldecoder.decode (Cookie.getvalue (), "Utf-8"); -}Catch(unsupportedencodingexception e) { the e.printstacktrace (); the                     } the                     if(isremove) {94Cookie.setmaxage (0); the Response.addcookie (cookie); the                     } the                 }98             } About         } -         returnvalue;101     }102}

It technology and industry exchange Group 417691667

Cookie Tool Class

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.