cookie获取java,在java中获取cookie值

I've initialized cooke like this in my JSP,

String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());

timeStamp = timeStamp + ":" + System.nanoTime();

String loc = "/u/poolla/workspace/FirstServlet/WebContent/WEB-INF/"+timeStamp;

Cookie thecookie = new Cookie("thecookie", loc);

thecookie.setMaxAge(60*60*24);

response.addCookie(thecookie);

So, all the cookies created will have the same name, but different value for different user access time

Question 1 - Solved,

in my.java I've used

String fpath = request.getParameter("thecookie").toString();

to get loc in my.java page, but this displayed null what is my mistake?

Answer : I found here the correct way to access cookie

Question 2,

at the ending of java servlet I want to deactivate/kill the cookie that has been passed at the beginning of java servlet and say if they are 5 users using the system in parallel how will I know which cookie to deactivate for which user in the last of my.java?

for example, consider

User 1 : cookie name `thecookie` value `AAA`

User 2 : cookie name `thecookie` value `BBB`

User 3 : cookie name `thecookie` value `CCC`

User 4 : cookie name `thecookie` value `DDD`

User 5 : cookie name `thecookie` value `EEE`

Now, at the end of my.java servlet I want to deactivate the cookie with value CCC that has been assigned at the beginning of my.java without disturbing other cookie values, how to do this?

解决方案

You are using the wrong method for reading the cookies..

Cookie[] cookies = request.getCookies();

if (cookies != null) {

for (Cookie cookie : cookies) {

if (cookie.getName().equals("cookieName")) {

//do something

//value can be retrieved using #cookie.getValue()

}

}

}

use this. No need to detect the cookie for particular user just deactivate it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值