我有一个安全 session cookie 集。我知道它在那里,因为我在 Chrome 开发者工具控制台和 Firebug 上看到它在 Firefox 中。
当我尝试从 JSP 中读取它时做:
<%= session.getAttribute("cookie_name") %>
我总是得到 null
。
我尝试执行此操作的页面是:
在设置 cookie 的同一域中(在本例中为“localhost”)
安全 ( HTTPS )
如何读取 cookie 值?我做错了什么?
最佳答案
这是我使用的代码。
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);
}
关于java - 在 Java 中读取安全 session cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4675038/