http://www.cnblogs.com/muzongyan/archive/2010/08/30/1812552.html
java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的生命周期和cookie的路径问题。
建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下
1
2
3
4
|
HttpServletRequest request
HttpServletResponse response
Cookie cookie =
new
Cookie(
"cookiename"
,
"cookievalue"
);
response.addCookie(cookie);
|
下面建立一个有生命周期的cookie,可以设置他的生命周期
1
2
3
4
5
6
7
8
|
cookie =
new
Cookie(
"cookiename"
,
"cookievalue"
);
cookie.setMaxAge(
3600
);
//设置路径,这个路径即该工程下都可以访问该cookie 如果不设置路径,那么只有设置该cookie路径及其子路径可以访问
cookie.setPath(
"/"
);
response.addCookie(cookie);
|
下面介绍如何读取cookie,读取cookie代码如下
1
2
3
4
5
|
Cookie[] cookies = request.getCookies();
//这样便可以获取一个cookie数组
for
(Cookie cookie : cookies){
cookie.getName();
// get the cookie name
cookie.getValue();
// get the cookie value
}
|
上面就是基本的读写cookie的操作。我们在实际中最好进行一下封装,比如增加一个cookie,我们关注的是cookie的name,value,生命周期,所以进行封装一个函数,当然还要传入一个response对象,addCookie()代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/**
* 设置cookie
* @param response
* @param name cookie名字
* @param value cookie值
* @param maxAge cookie生命周期 以秒为单位
*/
public
static
void
addCookie(HttpServletResponse response,String name,String value,
int
maxAge){
Cookie cookie =
new
Cookie(name,value);
cookie.setPath(
"/"
);
if
(maxAge>
0
) cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
|
读取cookie的时候,为了方便我们的操作,我们希望封装一个函数,只要我们提供cookie的name,我们便可以获取cookie的value,带着这个想法,很容易想到将cookie封装到Map里面,于是进行下面的封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/**
* 根据名字获取cookie
* @param request
* @param name cookie名字
* @return
*/
public
static
Cookie getCookieByName(HttpServletRequest request,String name){
Map<String,Cookie> cookieMap = ReadCookieMap(request);
if
(cookieMap.containsKey(name)){
Cookie cookie = (Cookie)cookieMap.get(name);
return
cookie;
}
else
{
return
null
;
}
}
/**
* 将cookie封装到Map里面
* @param request
* @return
*/
private
static
Map<String,Cookie> ReadCookieMap(HttpServletRequest request){
Map<String,Cookie> cookieMap =
new
HashMap<String,Cookie>();
Cookie[] cookies = request.getCookies();
if
(
null
!=cookies){
for
(Cookie cookie : cookies){
cookieMap.put(cookie.getName(), cookie);
}
}
return
cookieMap;
}
|
示例:
/**
* @Description: 把工单号放入Cookie中
* @param @param request
* @param @param response
* @param @param ddhm
* @return void
*/
private void setCookieByID(HttpServletRequest request,HttpServletResponse response,String name, Integer ddhm) {
String orderID = ""; //把订单号放在Cookie里面
Cookie cookie = getCookieByName(request,name);
if(null != cookie){
orderID = cookie.getValue();
orderID += "," + ddhm.toString();
cookie.setMaxAge(0); //把之前的Cookie失效
addCookie(response,name, orderID,24*3600);
logger.info("放在Cookie里的工单号:" +orderID);
}else{
addCookie(response,name, ddhm.toString(),24*3600);
logger.info("放在Cookie里的工单号:" + ddhm.toString());
}
}
/**
* 设置cookie
* @param response
* @param name cookie名字
* @param value cookie值
* @param maxAge cookie生命周期 以秒为单位
*/
private void addCookie(HttpServletResponse response,String name,String value,int maxAge){
Cookie cookie = new Cookie(name,value);
cookie.setPath("/");
if(maxAge>0) cookie.setMaxAge(maxAge);
// cookie.setDomain("feiren.com"); //设置域名
response.addCookie(cookie);
}
/**
* 根据名字获取cookie
* @param request
* @param name cookie名字
* @return
*/
private Cookie getCookieByName(HttpServletRequest request,String name){
Map<String,Cookie> cookieMap = readCookieMap(request);
if(cookieMap.containsKey(name)){
Cookie cookie = (Cookie)cookieMap.get(name);
return cookie;
}else{
return null;
}
}
/**
* 将cookie封装到Map里面
* @param request
* @return
*/
private Map<String,Cookie> readCookieMap(HttpServletRequest request){
Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
Cookie[] cookies = request.getCookies();
if(null!=cookies){
for(Cookie cookie : cookies){
cookieMap.put(cookie.getName(), cookie);
}
}
return cookieMap;
}
* @Description: 把工单号放入Cookie中
* @param @param request
* @param @param response
* @param @param ddhm
* @return void
*/
private void setCookieByID(HttpServletRequest request,HttpServletResponse response,String name, Integer ddhm) {
String orderID = ""; //把订单号放在Cookie里面
Cookie cookie = getCookieByName(request,name);
if(null != cookie){
orderID = cookie.getValue();
orderID += "," + ddhm.toString();
cookie.setMaxAge(0); //把之前的Cookie失效
addCookie(response,name, orderID,24*3600);
logger.info("放在Cookie里的工单号:" +orderID);
}else{
addCookie(response,name, ddhm.toString(),24*3600);
logger.info("放在Cookie里的工单号:" + ddhm.toString());
}
}
/**
* 设置cookie
* @param response
* @param name cookie名字
* @param value cookie值
* @param maxAge cookie生命周期 以秒为单位
*/
private void addCookie(HttpServletResponse response,String name,String value,int maxAge){
Cookie cookie = new Cookie(name,value);
cookie.setPath("/");
if(maxAge>0) cookie.setMaxAge(maxAge);
// cookie.setDomain("feiren.com"); //设置域名
response.addCookie(cookie);
}
/**
* 根据名字获取cookie
* @param request
* @param name cookie名字
* @return
*/
private Cookie getCookieByName(HttpServletRequest request,String name){
Map<String,Cookie> cookieMap = readCookieMap(request);
if(cookieMap.containsKey(name)){
Cookie cookie = (Cookie)cookieMap.get(name);
return cookie;
}else{
return null;
}
}
/**
* 将cookie封装到Map里面
* @param request
* @return
*/
private Map<String,Cookie> readCookieMap(HttpServletRequest request){
Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
Cookie[] cookies = request.getCookies();
if(null!=cookies){
for(Cookie cookie : cookies){
cookieMap.put(cookie.getName(), cookie);
}
}
return cookieMap;
}