简体   繁体   中英

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. Due to some issues, I want to change this to explicitly set the cookie using addHeader("Set-Cookie", cookieStr) . 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. When I call .toString() on the cookie object, it just gives me the object reference. 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.

The Cookie class has no such method.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM