如何在servlet中调用javascript cookie?

时间:2013-06-06 09:32:50

标签: java javascript servlets

我尝试使用

从javascript到servlet获取cookie
  

request.getcookie()

方法。但我发现该值为null。

有没有办法从javascript到servlet获取cookie?

3 个答案:

答案 0 :(得分:2)

试试这个

Cookie[] cookies = request.getCookies();


for (int i = 0; i < cookies.length; i++) {
    Cookie c = cookies[i];
     c.getName()  //cookie name
    c.getValue()   //cookie value

  }

答案 1 :(得分:0)

for (Cookie c : request.getCookies()) {
   if("myCookieName".equals(c.getName()) {
     System.out.println(c.getValue());
   }
}

答案 2 :(得分:0)

试试这个

Cookie[] cookie = request.getCookies();
if(cookie != null && cookie.length > 0) {
    System.out.println("CkInfo");
    for(int i = 0;i < cookie.length; ++i) {
    System.out.println("["+i+"] info");
    System.out.println("Name: "+cookie[i].getName());
    System.out.println("Comment: "+cookie[i].getComment());
    System.out.println("Domain: "+cookie[i].getDomain());
    System.out.println("MaxAge: "+cookie[i].getMaxAge());
    System.out.println("Path: "+cookie[i].getPath());
    System.out.println("Value: "+cookie[i].getValue());
    }
}