Java Servlet API 2.5 Cookie.getDomain()始终返回null [英] Java Servlet API 2.5 Cookie.getDomain() always returns null

查看:596
本文介绍了Java Servlet API 2.5 Cookie.getDomain()始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Tomcat上的Servlet API 2.5的Cookie类时遇到问题。我从HttpServletRequest对象中取出cookie列表,并像下面这样迭代:

I'm having an issue using the Cookie class of the Servlet API 2.5 on Tomcat . I pull out the list of cookies from the HttpServletRequest object and iterate over them like so:

Cookie[] cookies = request.getCookies();

for(Cookie cookie : cookies) {
    System.out.println("Name=" + cookie.getName() + " Domain=" + cookie.getDomain());
}

但是,对于请求中的每个cookie,为什么是这样?我要求的原因是因为我有一个相同名称的cookie在两个不同的域,我想要能够基于域之间进行区分。为了帮助澄清情况,我的相同名称的cookie是设置在.anydomain.net和.subdomain.anydomain.net。两者都在请求中发送,但是当他们到达servlet时域是空的。是servlet不能看到发送到它的cookie的域的预期行为吗?

However, for every single cookie in the request the Domain is null. Why is this? The reason I'm asking is because I have a cookie with the same name in two different domains and I want to be able to differentiate between them based on the domain. To help clarify the situation, my identically named cookies are being set in .anydomain.net and .subdomain.anydomain.net. Both are getting sent in the request but the domains are null when they get to the servlet. Is it expected behavior that the servlet cannot see the domain of cookies sent to it?

编辑:我在前一个请求中设置cookie与域,过期和路径到servlet。使用这些cookie进入浏览器的下一个请求会将该域显示为null。

I set the cookies along with domain,expiration,and path in a previous request to the servlet. The next request coming into the browser with these cookies shows the domain as null. I have verified the cookies are getting set in the right domains in the browser.

编辑2:我使用的是Tomcat 6

Edit 2: I'm using Tomcat 6

推荐答案

确定您可以从请求Cookie中获得任何值,但不包括值?
浏览器将只在HTTP Cookie头中发送name = value。

Are you sure that you can get anything except the value from request cookies? The browser will send only name=value in the HTTP Cookie header.

其他属性(安全,域,路径,过期)你自己设置响应。
它们用于创建Set-Cookie响应头。

Other attributes (secure, domain, path, expiration) are only available for cookies that you set into the response yourself. They are used to create the Set-Cookie response headers.

这篇关于Java Servlet API 2.5 Cookie.getDomain()始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆