简体   繁体   English

Java HttpServletResponse addCookie在chrome和ie上无用

[英]Java HttpServletResponse addCookie could not be useful on chrome and ie

I use addCookie method to set scdid which like a jsessionid . 我使用addCookie方法来设置像jsessionid一样的scdid But unfortunately, it's not useful on Chrome and IE which is ok on Firefox. 但不幸的是,它在Chrome和IE上没有用,这在Firefox上是可以的。

So, could anybody help me? 那么,任何人都可以帮助我吗?

Cookie cookie = new Cookie("xx", "xxx");
cookie.setMaxAge(3600);
cookie.setDomain("xxxx"); 
cookie.setPath("/");
response.addCookie(cookie);

Here is request/response body: 这是请求/响应机构:

Headers
Remote Address:127.0.0.1:80
Request URL:http(can't give it to a link)://localhost/login
Request Method:POST
Status Code:302 Found

Request body
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:28
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=3a210ab5-2e48-4a0b-b669-f9b5e82b9988
Host:localhost
Origin:http://localhost
Referer:http://localhost/login
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36

Response body
Content-Length:0
Date:Tue, 09 Jun 2015 01:06:52 GMT
Location:http://localhost/
Server:Apache-Coyote/1.1
Set-Cookie:examid=366d69ae-5249-4e68-b779-c03056188249; Domain=localhost; Expires=Tue, 09-Jun-2015 02:06:51 GMT; Path=/

The above of that response is received at Tue, 09-Jun-2015 08:06:51 以上回复是在星期二,2015年6月9日08:06:51收到的

2015-06-10 EDIT: I have solved this problem by follows way, but I still confused. 2015-06-10编辑:我已按照以下方式解决了这个问题,但我仍然感到困惑。

Cookie cookie = new Cookie("examid", UUID.randomUUID().toString());
response.addCookie(cookie);

In this solution, I don't set expires, domain and path, which get inspiration from jsessionid in Chrome. 在这个解决方案中,我没有设置expires,domain和path,它们从Chrome中的jsessionid中获取灵感。

Remote Address:127.0.0.1:80
Request URL:http://localhost/login
Request Method:POST
Status Code:302 Found

Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:28
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=f63df7a3-f381-4914-92c1-a349bf73316b; examid=
Host:localhost
Origin:http://localhost
Referer:http://localhost/login
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36

Form Dataview source
username:admin
password:xxxx

Response Headers
Content-Length:0
Date:Wed, 10 Jun 2015 01:35:53 GMT
Location:http://localhost/
Server:Apache-Coyote/1.1
Set-Cookie:examid=d65f7974-17f1-4338-9284-48f00670a012

2015-06-12 EDIT: I set MaxAge, path, domain one by one. 2015-06-12编辑:我逐一设置MaxAge,路径,域名。 And I found it works error when set domain (which from request parameter). 我发现设置域(来自请求参数)时它会出错。 There is my code that how to get domain: 有我的代码如何获取域:

private static final String getDomainName(HttpServletRequest request) {
    String domainName = null;

    String serverName = request.getRequestURL().toString();
    if (serverName == null || serverName.equals("")) {
        domainName = "";
    } else {
        serverName = serverName.toLowerCase();
        serverName = serverName.substring(7);
        final int end = serverName.indexOf("/");
        serverName = serverName.substring(0, end);
        final String[] domains = serverName.split("\\.");
        int len = domains.length;
        if (len > 3) {
            // www.xxx.com.cn
            domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
        } else if (len <= 3 && len > 1) {
            // xxx.com or xxx.cn
            domainName = "." + domains[len - 2] + "." + domains[len - 1];
        } else {
            domainName = serverName;
        }
    }

    if (domainName != null && domainName.indexOf(":") > 0) {
        String[] ary = domainName.split("\\:");
        domainName = ary[0];
    }
    return domainName;
}

Answer for the updated question: 回答更新的问题:

Actually, I get domain "localhost" by my code 实际上,我通过我的代码得到域“localhost”

So this is the problem: the cookie will not be set if your domain name = localhost. 所以这就是问题:如果您的域名= localhost,则不会设置cookie。 By the spec , cookie domain name must have at least 2 or 3 dots (.). 根据规范 ,cookie域名必须至少有2或3个点(。)。 Localhost is a top-level domain name, so that will not work. Localhost是顶级域名,因此无法使用。 Failing to abide to this rule may result in problems in certain browsers: 不遵守此规则可能会导致某些浏览器出现问题:

Only hosts within the specified domain can set a cookie for a domain and domains must have at least two (2) or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us". 只有指定域中的主机可以为域设置cookie,并且域中必须至少有两(2)或三(3)个句点,以防止表单域:“。com”,“。hadu”和“ va.us”。

So as the solution, while working on localhost, I would recommend that you either don't set the domain name, or just use 127.0.0.1 . 因此,作为解决方案,在使用localhost时,我建议您不要设置域名,或者只使用127.0.0.1

You can refer to this answer and this answer for more information. 您可以参考此答案此答案以获取更多信息。

可能在您的Chrome和IE中禁用了Cookie?

HttpServletResponse - > flushBuffer()对我来说绝对没问题。

response.flushBuffer();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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