文法: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所使用的協議的版本號碼 |