日期:2014-05-18  浏览次数:20823 次

cookies取不到值?
写cookies
    Cookie   cookie1=new   Cookie( "login_name ", "guest ");
                  Cookie   cookie2=new   Cookie( "login_pass ",strPwd);
                  System.out.println( "====================== ");
                  cookie1.setMaxAge(60*60*24);
                  cookie2.setMaxAge(60*60*24);
  response.addCookie(cookie1);
                                          response.addCookie(cookie2);


读cookies


Cookie[]   cookie=request.getCookies();
String   name= " ",pass= " ";
if(cookie!=null){

for(int   i=0;i <cookie.length;i++){
System.out.println(cookie.length);
System.out.println(cookie[0].getName()+   "=== "   +   cookie[0].getValue().toString());
  if(cookie[i].getName().equals( "login_name ")){
  System.out.println( "-------------------------------- ");  
    name=cookie[i].getValue();
    System.out.println( "yxy   test   name:   "   +   name);
    }
   
  if(cookie[i].getName().equals( "login_pass ")){
    pass=cookie[i].getValue();
      System.out.println( "yxy   test   pass:   "   +   pass);
      }
}
}

为什么取不到值   ,取到了session   cookies,没有取到本地的cookies?本地cookies已经生成,输出结果

1
JSESSIONID===5396180930C5EE179DA0F524008E46D9
如何才能取到本地的cookies,另外我本地的IE设置隐私级别为最低.


------解决方案--------------------
<%@ page contentType= "text/html;charset=gb2312 " %>
<%
//写cookies
Cookie cookie1=new Cookie( "login_name ", "guest ");
Cookie cookie2=new Cookie( "login_pass ", "strPwd ");
System.out.println( "====================== ");
cookie1.setMaxAge(60*60*24);
cookie2.setMaxAge(60*60*24);
response.addCookie(cookie1);
response.addCookie(cookie2);


//读cookies


Cookie[] cookie=request.getCookies();
String name= " ",pass= " ";
if(cookie!=null){
for(int i=0;i <cookie.length;i++){
System.out.println(cookie.length);
System.out.println(cookie[0].getName()+ "=== " + cookie[0].getValue().toString());
if(cookie[i].getName().equals( "login_name ")){
name=cookie[i].getValue();
System.out.println( "login_name= "+name);
}
if(cookie[i].getName().equals( "login_pass ")){
pass=cookie[i].getValue();
System.out.println( "login_pass= "+pass);
}
}
}
else{
System.out.println( "cookie==null ");
}
%>
结果是:
======================
3
login_name===guest
login_name=guest
3
login_name===guest
login_pass=strPwd
3
login_name===guest
看看有什么不同?
------解决方案---------