5

我尝试使用从 javascript 获取 cookie 到 servlet

请求.getcookie()

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

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

4

3 回答 3

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

  }
于 2013-06-06T09:34:29.217 回答
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());
    }
}
于 2013-06-06T10:22:49.513 回答
0
for (Cookie c : request.getCookies()) {
   if("myCookieName".equals(c.getName()) {
     System.out.println(c.getValue());
   }
}
于 2013-06-06T09:42:54.570 回答