重定向 url cookie

想象下面两行伪代码:

1. setCookie(...);
2. redirect(new_web_url);

首先设置一个Cookie,然后重定向到另外一个网址,这个网址跟当前网站的域名不同

在多数情况下这两行代码执行毫无问题,cookie 正确保存到浏览器,页面也跳转到了新的url上。

但是有很多firefox浏览器下可能出现的问题是cookie无法写入,而 url 跳转成功。还不一定能重现出来这种问题,但发生的概率非常高。

解决的办法:

不要直接 redirect 到另外的网址,而改用:

1
2
3
String new_url = "http://www.oschina.net/";
String html = "<script type='text/javascript'>location.href='"+new_url+"';</script>";
response.getWriter().print(html);

如此 cookie 就可以保证正确写到浏览器,然后执行页面跳转。

原文地址:https://www.cnblogs.com/rongfengliang/p/5944594.html