随笔-124  评论-49  文章-56  trackbacks-0
//servlet
//生成cookie对名象
java.util.Date date=new java.util.Date();
Cookie c
=new Cookie("lastVisited",date.toString());
c.setMaxAge(
60*60*24);//设置有效时间,单位秒
c.setPath("/");//设置路径
c.setDomain(".v512.com");//设置域名
//以上都有对应的get方法
response.addCookie(c);//保存对象到用户机硬盘上

//读取Cookie
Cookie[] cookies=request.getCookies();
Cookie c
=null;
if(cookies!=null){
        
for(int i=0;i<cookies.lenght;i++){
                c
=cookies[i];
                out.println(
"cookie name:"+c.getName());
                out.println(
"cookie value:"+c.getValue());
        }

}


//修改Cookie
Cookie[] cookies=reuqest.getCookies();
Cookie c
=null;
if(cookies!=null){
        
for(int i=0;i<cookies.lenght;i++){
                c
=cookies[i];
                
if(c.getName().equals("lastVisited")){
                        c.setValue(
"2007-10-30");
                        c.setMaxAge(
60*60*24);
                        response.addCookie(c);
                }

        }

}


//删除Cookie
Cookie[] cookies=reuqest.getCookies();
Cookie c
=null;
if(cookies!=null){
        
for(int i=0;i<cookies.lenght;i++){
                c
=cookies[i];
                
if(c.getName().equals("lastVisited")){
                        c.setMaxAge(
0);
                        response.addCookie(c);
                }

        }

}
posted on 2009-11-03 10:25 junly 阅读(192) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: