创建会话时添加另一个cookie [英] Add another cookie when session is created

查看:56
本文介绍了创建会话时添加另一个cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以struts 2(javax.servlet v 2.5和嵌入式tomcat lib)开发的Web应用程序中,在创建HttpSession时(第一次调用请求时),在第一个请求之后,我总是将JSESSIONID始终自动添加到cookie中.getSession()),我知道JSESSIONID旨在标识创建的http会话.

In my web application developed in struts 2 (javax.servlet v 2.5 and an embedded tomcat lib), I have the JSESSIONID always automatically added to the cookies after the first request when a HttpSession is created (at first call to request.getSession()), I know that the JSESSIONID aims to identify the created http session.

现在我想添加更多cookie,但是我没有找到如何在struts 2框架中添加此cookie?以及用户注销后如何将其从响应的Cookie中删除.

Now I want to add more cookies but I don't found how to adding this cookie in the framework struts 2? and how can I remove it from the cookie of the response when user logged out.

推荐答案

添加cookieName Cookie:

Add cookieName Cookie:

Cookie newCookie = new Cookie("cookieName", "cookieValue");
newCookie.setMaxAge(60*60*24*365); //Store cookie for 1 year
response.addCookie(newCookie);

然后将其删除:

Cookie[] cookies = request.getCookies();

if (cookies != null) {
 for (Cookie cookie : cookies) {
   if (cookie.getName().equals("cookieName")) {
     cookie.setValue("");
     cookie.setPath("/");
     cookie.setMaxAge(0);
     resp.addCookie(cookie);
  }
 }     
}

这篇关于创建会话时添加另一个cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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