Cookies for common tool classes

Source: Internet
Author: User
Tags setcookie
Package com.chenfeng.ego.commons.util;
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 and does not encode * * @param reques T * @param cookiename * @return/public static String Getcookievalue (HttpServletRequest request, Stri
    Ng CookieName) {return getcookievalue (request, cookiename, false); /** * Get the value of the cookie * * @param request * @param cookiename * @return * * Public STA Tic string 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 = Urldecoder.decode (Cookielist[i].getvalue (), utf-
                    8 ");
                    else {retvalue = Cookielist[i].getvalue ();
                } break;
        A catch (Unsupportedencodingexception e) {e.printstacktrace ());
    return retvalue; /** * Get the value of the cookie * * @param request * @param cookiename * @return * * Public STA Tic string Getcookievalue (HttpServletRequest request, String cookiename, String encodestring) {cookie[] Cookielis
        t = 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)) {RetValue = Urldecoder.decode (
                    Cookielist[i].getvalue (), encodestring);
                Break
        A catch (Unsupportedencodingexception e) {e.printstacktrace ());
    return retvalue; /** * Set the value of the cookie does not set the effective time default browser shutdown is invalid, also does not encode */public static void Setcookie (HttpServletRequest request, H Ttpservletresponse response, String cookiename, String cookievalue) {Setcookie (Request, Response, COO
    Kiename, 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, Httpservle Tresponse response, String cookiename, string cookievalue, int cookiemaxage) {Setcookie (Request, RESP
    Onse, CookieName, Cookievalue, Cookiemaxage, false); /** * Setting the value of the cookie does not set the effective time, but the encoding/public static void SetcoOkie (HttpServletRequest request, httpservletresponse 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, the encoding parameter */public static void Setcookie (HttpServletRequest request, HTTPSERVL Etresponse response, String cookiename, string cookievalue, int cookiemaxage, Boolean isencode) {dose
    Tcookie (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, HTT
        Pservletresponse response, String cookiename, string cookievalue, int cookiemaxage, string encodestring) {
    Dosetcookie (Request, Response, CookieName, Cookievalue, Cookiemaxage, encodestring); /** * Delete cookie with cookie domain name/public static void Deletecookie (HttpServletRequest request, HttpservletresPonse response, String cookiename) {Dosetcookie (Request, Response, CookieName, "",-1, false);  /** * Sets the value of the cookie and makes it effective within a specified time * * @param the maximum number of seconds that the Cookiemaxage cookie is in effect * * private static final void Dosetcookie (HttpServletRequest request, httpservletresponse Response, String cookiename, String Cookieva Lue, int cookiemaxage, Boolean isencode) {try {if (Cookievalue = = null) {Cookievalu
            e = "";
            else if (isencode) {cookievalue = Urlencoder.encode (Cookievalue, "utf-8");
            The cookie cookie = new Cookie (cookiename, cookievalue);
            if (Cookiemaxage > 0) cookie.setmaxage (cookiemaxage);
            	if (null!= request) {//Set domain name 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 ();  /** * Sets the value of the cookie and makes it effective within a specified time * * @param the maximum number of seconds that the Cookiemaxage cookie is in effect * * private static final void Dosetcookie (HttpServletRequest request, httpservletresponse Response, String cookiename, String Co Okievalue, int cookiemaxage, String encodestring) {try {if (Cookievalue = = null) {C
            Ookievalue = "";
            else {cookievalue = Urlencoder.encode (Cookievalue, encodestring);
            The cookie cookie = new Cookie (cookiename, cookievalue);
            if (Cookiemaxage > 0) cookie.setmaxage (cookiemaxage);
            	if (null!= request) {//Set domain name cookie String domainname = getdomainname (request);
          SYSTEM.OUT.PRINTLN (domainname);      if (!)
                LocalHost ". Equals (domainname)) {//Do not write when local test. When the actual release is in the Open//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) {

        String domainname = null;
        String serverName = Request.getrequesturl (). toString ();
        if (ServerName = = NULL | | servername.equals ("")) {domainname = "";
            else {final int end = Servername.lastindexof ("/");
            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[le N-2] + "." + DomainS[LEN-1]; else if (Len <= 3 && len > 1) {//xxx.com or xxx.cn domainname = "." + Do
            Mains[len-2] + "." + domains[len-1];
            else {domainname = ServerName; } if (domainname!= null && domainname.indexof (":") > 0) {string[] ary = domain
            Name.split ("\:");
        DomainName = ary[0];
    return domainname;
 }

}

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.