整理了一个在Servlet中对Cookie增删改查的工具类,首先要注意的是在服务器端是无法对Cookie做修改的,只能做到覆盖创建。
引用StackOverflow上James Sumners的回答:
Per section 3.3.4 of RFC 2965, the user agent does not include the expiration information in the cookie header that is sent to the server. Therefore, there is no way to update an existing cookie’s value while retaining the expiration date that was initially set based solely on the information associated with the cookie.
So the answer to this question is: you can’t do that.
工具类如下:
1 | package com.demo.util; |