最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java cookie操作与详细参数说明
时间:2010-12-19 编辑:简简单单 来源:一聚教程网
语法: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所使用的协议的版本号 |
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24