'How to call a javascript cookie in servlet?
I have tried getting cookie from javascript to servlet using
request.getcookie()
method. But I found the value as null.
Is there any way to get a cookie from javascript to servlet?
Solution 1:[1]
try this
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie c = cookies[i];
c.getName() //cookie name
c.getValue() //cookie value
}
Solution 2:[2]
for (Cookie c : request.getCookies()) {
if("myCookieName".equals(c.getName()) {
System.out.println(c.getValue());
}
}
Solution 3:[3]
Try this
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());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PSR |
| Solution 2 | mauretto |
| Solution 3 | NamingException |
