在java中添加cookie,然后HTTP重定向不在客户端显示cookie [英] Adding cookie in java and then HTTP redirect doesnt show cookie in client side

查看:204
本文介绍了在java中添加cookie,然后HTTP重定向不在客户端显示cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求我在哪里需要在java中添加cookie,然后重定向到不同的URL。现在这个url进程应该保持我设置的cookie,并在其处理后发送回客户端。代码如下

I have requirement where in i need to add cookie in java and then redirect it to different URL. Now this url process should persist the cookie which i set and after its processing send it back to client. The code goes as follows

Cookie cookie = new Cookie("name", "value")
// To make sure cookie is established for all the url paths
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
response.sendRedirect("someNewUrl");

请帮助我如何在整个重定向生命周期和客户端保持cookie。提前感谢。

Please help me regarding how can i persist the cookie throughout the redirect lifecycle and to the client. Thanks in advance.

推荐答案

尝试实际将添加到响应中

Try to actually add the cookie to the response:

Cookie cookie = new Cookie("user", "anonymous");
response.addCookie(cookie);

另请参阅:

  • javax.servlet.http.HttpServletResponse.addCookie(javax.servlet.http.Cookie)

这篇关于在java中添加cookie,然后HTTP重定向不在客户端显示cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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