给定一个javax.servlet.http.Cookie对象,是否有一种方法可以将Cookie格式化为http友好格式,以便在响应中发送它?同样,如果请求中有cookie头,是否有方法将cookie解析为javax.servlet.http.cookie对象?我找遍了所有的东西,但找不到能做到这一点的方法。
谢谢!
您可以将HttpCookie类用于自己的实现。将其toString()
功能用于
Constructs a cookie header string representation of this cookie, which is in the format defined by corresponding cookie specification, but without the leading "Cookie:" token.
在这之后,将头Set-Cookie:THE_TO_STRING_VALUE
添加到响应中,就这样。
为了读取cookie,您需要解析headers
。