[英]Convert a Java cookie object to a String to set in httpResponse "Set-Cookie" header
We originally used Java's HttpsServletResponse's addCookie(Cookie cookie)
method to add a self-created cookie to our servlet response object.我们最初使用 Java 的 HttpsServletResponse 的
addCookie(Cookie cookie)
方法将自创建的 cookie 添加到我们的 servlet 响应对象中。 Due to some issues, I want to change this to explicitly set the cookie using addHeader("Set-Cookie", cookieStr)
.由于某些问题,我想将其更改为使用
addHeader("Set-Cookie", cookieStr)
显式设置 cookie。 The cookie is constructed by passing fields to a cookie object, and I wanted to know if I can use that object to just spit out a string. cookie 是通过将字段传递给 cookie 对象来构造的,我想知道我是否可以使用该对象来仅吐出一个字符串。 When I call
.toString()
on the cookie object, it just gives me the object reference.当我在 cookie 对象上调用
.toString()
时,它只是给了我对象引用。 I know I can just construct the string, but since I already have the object, I wanted to know if there was a simple method I could call on the cookie object to parse it to a string, rather than create it myself.我知道我可以只构造字符串,但是因为我已经有了对象,所以我想知道是否有一个简单的方法可以调用 cookie 对象来将其解析为字符串,而不是自己创建它。
The Cookie class has no such method. Cookie 类没有这样的方法。
You could take a look at an existing implementation to judge whether you want to do that yourself.您可以查看现有的实现来判断您是否想自己做。 Take a look at the Rfc6265CookieProcessor from Apache Tomcat.
看看来自 Apache Tomcat 的Rfc6265CookieProcessor 。 A more simple and less complete approach is here .
一个更简单和不完整的方法是在这里。 That's quite some effort.
这是相当的努力。
The better way is probably to solve the issues you had with the addCookie(Cookie cookie)
method.更好的方法可能是解决您在使用
addCookie(Cookie cookie)
方法时遇到的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.