java cookie操作與詳細參數說明

來源:互聯網
上載者:User

文法:cookie 變數名稱.setmaxage(有效周期);

  有效周期的時間以秒為單位,時間設定越大,表示cookie對象的有效時間越長,如果把有效周期設定為0,則表示此cookie對象存放在瀏覽器後將立即失效,如果把有效周期設定為任意一個負數,則當瀏覽器關閉後,此cookie對象立即失效。

  cookie類的所有屬性操作方法如表所示。


看一執行個體

import java.io.unsupportedencodingexception;

import javax.servlet.http.cookie;
import javax.servlet.http.https教程ervletrequest;
import javax.servlet.http.httpservletresponse;

/**
* @author administrator
*
* to change the template for this generated type comment go to
* window>preferences>java>code generation>code and comments
*/
public class cookies {

    public int maxage; // 設定該cookie的有效期間,單位為秒
    public string path; // cookie路徑
    cookie[] cookie_get = {};

    public cookies() {
        maxage = -1;
        path = "/";
    }

    /**
    * put cookie to the client
    *
    * @param response
    * @param name
    * @param value
    */
    public void putcookie(httpservletresponse response, string name,
            string value) {
        try {
            cookie cookie = new cookie(name, encode(value));
            cookie.setmaxage(maxage);
            cookie.setpath(path);
            response.addcookie(cookie);
        } catch (exception e) {
            e.printstacktrace();
        }
    }

    /**
    * get cookie from client
    *
    * @param request
    * @param name
    * @return
    */
    public string getcookie(httpservletrequest request, string name) {
        if (cookie_get == null || cookie_get.length == 0) {
            cookie_get = request.getcookies();
        }
        string returnstr;
        returnstr = null;
        try {
            for (int i = 0; cookie_get != null && i < cookie_get.length; i++) {
                cookie_get[i].setpath(path);
                if (cookie_get[i].getname().equals(name)) {
                    cookie_get[i].setmaxage(-1);
                    returnstr = cookie_get[i].getvalue().tostring();
                    break;
                }
            }
            return decode(returnstr);
        } catch (exception e) {
            return decode(returnstr);
        }
    }

    /**
    * 清除cookie
    *
    * @param response
    *            httpservletresponse
    * @param name
    *            string
    */
    public void removecookie(httpservletresponse response, string name) {
        putcookie(response, name, null);
    }

    /**
    * 對給定字元進行 url 解碼
    *
    * @param value
    *            string
    * @return string
    */
    private static string decode(string value) {
        string result = "";
        if (!isempty(value)) {
            try {
                result = java.net.urldecoder.decode(value, "gbk");
            } catch (unsupportedencodingexception ex) {

            }
        }
        return result;
    }

    /**
    * 對給定字元進行 url 編碼
    *
    * @param value
    *            string
    * @return string
    */
    private static string encode(string value) {
        string result = "";
        if (!isempty(value)) {
            try {
                result = java.net.urlencoder.encode(value, "gbk");
            } catch (unsupportedencodingexception ex) {

            }
        }
        return result;
    }

    /**
    * 判斷是否為空白,為空白返回true
    *
    * @param value
    *            string
    * @return boolean
    */
    private static boolean isempty(string value) {
        if (value == null || value.trim().equals(""))
            return true;
        else
            return false;
    }

    /**
    * 查檢二個資料是否為空白,如果為空白返回true
    *
    * @param value1
    * @param value2
    * @return
    */
    public boolean isempty(string value1, string value2) {
        if (null == value1 || null == value2 || "".equals(value1)
                || "".equals(value2))
            return true;
        else
            return false;
    }
}

cookie類的所有屬性操作方法如表所示。

方法 意 義
cookie(string, string) 產生一個有名和值的cookie
clone() 返回當前對象的一個拷貝
getcomment() 返回描述該cookie的注釋,沒有就為空白
getdomain() 返回該cookie的網域名稱
getmaxage() 返回該cookie的最大壽命
getname() 返回該cookie的名字
getpath() 返回使用該cookie的所有url首碼
getsecure() 返回該cookie的安全標誌
getvalue() 返回該cookie的值
getversion() 返回該cookie的版本
setcomment(string) 設定描述該cookie的注釋
setdomain(string) 設定該cookie的網域名稱
setmaxage(int) 設定該cookie的最大壽命
setpath(string) 設定該cookie只能被從使用該url首碼的請求提出
setsecure(boolean) 設定該cookie的安全標誌
setvalue(string) 設定該cookie的值
setversion(int) 設定該cookie所使用的協議的版本號碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.