Read a secure session cookie in Java
I have a secure session cookie set开发者_如何学Python. I know it's there since I'm seeing it on the Chrome Developer Tools console and on Firebug in Firefox.
When I try to read it from a JSP doing:
<%= session.getAttribute("cookie_name") %>
I always get null
.
The page from which I'm trying to do this is:
On the same domain in which the cookie is set (in this case 'localhost')
Secured (HTTPS)
How do I read the cookie value? What am I doing wrong?
Here is the code I use.
public static String getCookieValue(HttpServletRequest request, String name)
{
boolean found = false;
String result = null;
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
int i = 0;
while (!found && i < cookies.length)
{
if (cookies[i].getName().equals(name))
{
found = true;
result = cookies[i].getValue();
}
i++;
}
}
return (result);
}
Just as a clarification, I thought that you had to access session-lived cookies using the session
object.
This is not like that, as Milhous pointed correctly, session-lived cookies are accessed like any other cookie
精彩评论