1

我有一个安全会话 cookie 集。我知道它就在那里,因为我在 Chrome 开发者工具控制台和Firefox中的Firebug上看到了它。

当我尝试从JSP中读取它时:

<%= session.getAttribute("cookie_name") %>

我总是得到null

我试图这样做的页面是:

  • 在设置 cookie 的同一域上(在本例中为“localhost”)

  • 安全(HTTPS

如何读取 cookie 值?我究竟做错了什么?

4

2 回答 2

4

这是我使用的代码。

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);
    }
于 2011-01-12T23:13:04.320 回答
2

澄清一下,我认为您必须使用该session对象访问会话中的 cookie。

这不是那样的,正如 Milhous 正确指出的那样,会话中的 cookie 像任何其他 cookie 一样被访问

于 2011-01-12T23:22:19.697 回答