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