gudii9
asked on
servlet cookie finding by name
Hi,
i was reading on servlet cookie as below
http://tutorials.jenkov.com/java-servlets/cookies.html
i wonder why there is no way to find cookie by its name rather than iterating all the array?
i was reading on servlet cookie as below
http://tutorials.jenkov.com/java-servlets/cookies.html
Now you can iterate through the array of cookies and find the cookies you need. Unfortunately there is no way to obtain a cookie with a specific name. The only way to find that cookie again is to iterate the Cookie[] array and check each cookie name. Here is an example:
i wonder why there is no way to find cookie by its name rather than iterating all the array?
If you need to access more than one cookie, you could iterate the Cookie[] array once, and put the Cookie instances into a Map, using the cookie name as key, and the Cookie instance as value. Here is how that could look:what is the cookie.getName() gets? from where it gets this cookie name. I was not clear. please advise
Map cookieMap = new HashMap();
Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies){
cookieMap.put(cookie.getName(), cookie);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.