cookie

// 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);
                }

        }

}

你可能感兴趣的:(C++,c,servlet,C#)