Java Learning---Cookie tool

Source: Internet
Author: User

Cookieutils.java

Import Java.io.unsupportedencodingexception;import Java.net.urldecoder;import Java.net.urlencoder;import Javax.servlet.http.cookie;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * * Cookie Tool class * */public final class Cookieutils {/** * Gets the value of the cookie, does not Code * * @param request * @param cookiename * @return */public static String Getcookievalue (Httpser    Vletrequest request, String cookiename) {return getcookievalue (request, cookiename, false); }/** * Gets the value of the cookie, * * @param request * @param cookiename * @return */public static Strin G Getcookievalue (HttpServletRequest request, String CookieName, Boolean isdecoder) {cookie[] cookielist = Request.        GetCookies ();        if (cookielist = = NULL | | cookiename = = NULL) {return null;        } String retvalue = null; try {for (int i = 0; i < cookielist.length; i++) {if (cOokielist[i].getname (). Equals (CookieName)) {if (isdecoder) {RetValue = Urldeco                    Der.decode (Cookielist[i].getvalue (), "UTF-8");                    } else {retvalue = Cookielist[i].getvalue ();                } break;        }}} catch (Unsupportedencodingexception e) {e.printstacktrace ();    } return RetValue; }/** * Gets the value of the cookie, * * @param request * @param cookiename * @return */public static Strin G Getcookievalue (HttpServletRequest request, String cookiename, String encodestring) {cookie[] cookielist = reques        T.getcookies ();        if (cookielist = = NULL | | cookiename = = NULL) {return null;        } String retvalue = null; try {for (int i = 0; i < cookielist.length; i++) {if (Cookielist[i].getname (). Equals (Cookie Name) {RetValue = URldecoder.decode (Cookielist[i].getvalue (), encodestring);                Break        }}} catch (Unsupportedencodingexception e) {e.printstacktrace ();    } return RetValue; }/** * Setting the value of the cookie does not set the effective time the default browser is disabled or not encoded */public static void Setcookie (HttpServletRequest request, Httpse Rvletresponse response, String cookiename, String cookievalue) {Setcookie (Request, Response, CookieName    , Cookievalue,-1); }/** * Sets the value of the cookie to take effect within a specified time, but does not encode */public static void Setcookie (HttpServletRequest request, HTTPSERVLETRESP Onse response, String cookiename, string cookievalue, int cookiemaxage) {Setcookie (Request, Response, C    Ookiename, Cookievalue, Cookiemaxage, false); }/** * Setting the value of the cookie does not set the effective time, but the encoding */public static void Setcookie (HttpServletRequest request, Httpservletrespon       SE Response, String cookiename, String Cookievalue, Boolean Isencode) { Setcookie (Request, Response, CookieName, Cookievalue,-1, Isencode); }/** * Sets the value of the cookie to take effect within a specified time, encoding parameter */public static void Setcookie (HttpServletRequest request, Httpservletres Ponse response, String cookiename, string cookievalue, int cookiemaxage, Boolean isencode) {Dosetcookie    (Request, response, CookieName, Cookievalue, Cookiemaxage, Isencode); }/** * Sets the value of the cookie to take effect within a specified time, encoding parameter (specified encoding) */public static void Setcookie (HttpServletRequest request, Httpserv Letresponse response, String cookiename, string cookievalue, int cookiemaxage, string encodestring) {do    Setcookie (Request, Response, CookieName, Cookievalue, Cookiemaxage, encodestring);  }/** * Delete cookies with cookie domain */public static void Deletecookie (HttpServletRequest request, HttpServletResponse    Response, String cookiename) {Dosetcookie (Request, Response, CookieName, "",-1, false);   }/** * Set the value of the cookie and make it effective within the specified time *   * @param the maximum number of seconds the Cookiemaxage cookie takes effect */private static final void Dosetcookie (HttpServletRequest request, Httpser Vletresponse response, String cookiename, string cookievalue, int cookiemaxage, Boolean isencode) {try            {if (Cookievalue = = null) {Cookievalue = "";            } else if (isencode) {cookievalue = Urlencoder.encode (Cookievalue, "utf-8");            } Cookie cookie = new Cookie (cookiename, cookievalue);            if (Cookiemaxage > 0) cookie.setmaxage (cookiemaxage);            if (null! = Request) {//Set the domain name of the cookie String domainName = getdomainname (request);                System.out.println (DomainName); if (! "                LocalHost ". Equals (DomainName)) {Cookie.setdomain (domainName);            }} cookie.setpath ("/");        Response.addcookie (cookie);        } catch (Exception e) {e.printstacktrace (); }    }    /** * Set the value of the cookie and make it effective within a specified time * * @param the maximum number of seconds the Cookiemaxage cookie takes effect */private static final void do Setcookie (HttpServletRequest request, httpservletresponse Response, String cookiename, string cookievalue, int            Cookiemaxage, String encodestring) {try {if (Cookievalue = = null) {Cookievalue = "";            } else {cookievalue = Urlencoder.encode (Cookievalue, encodestring);            } Cookie cookie = new Cookie (cookiename, cookievalue);            if (Cookiemaxage > 0) cookie.setmaxage (cookiemaxage);            if (null! = Request) {//Set the domain name of the cookie String domainName = getdomainname (request);                System.out.println (DomainName); if (! "                LocalHost ". Equals (DomainName)) {Cookie.setdomain (domainName);            }} cookie.setpath ("/");        Response.addcookie (cookie);    } catch (Exception e) {     E.printstacktrace (); }}/** * Gets the domain name of the cookie */private static final String Getdomainname (HttpServletRequest request) {S        Tring domainName = null;        String serverName = Request.getrequesturl (). toString ();        if (ServerName = = NULL | | servername.equals ("")) {domainName = "";            } else {serverName = Servername.tolowercase ();            ServerName = servername.substring (7);            Final int end = Servername.indexof ("/");            ServerName = servername.substring (0, end);            Final string[] domains = servername.split ("\ \");            int len = domains.length; if (Len > 3) {//www.xxx.com.cn DomainName = "." + domains[len-3] + "." + Domains[len            -2] + "." + domains[len-1]; } else if (Len <= 3 && len > 1) {//xxx.com or xxx.cn domainName = "." + DOMA            Ins[len-2] + "." + domains[len-1]; } else {domainName = ServerName; }} if (domainName! = null && domainname.indexof (":") > 0) {string[] ary = DomainName            . split ("\ \:");        DomainName = ary[0];    } return domainName; }}

Java Learning---cookie tool

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.