简体   繁体   中英

Read a secure session cookie in Java

I have a secure session cookie set. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM