Cookie usernameCookie = new Cookie ("username", attributeUsername);
usernameCookie.setMaxAge(24*60*60);开发者_开发知识库
response.addCookie(usernameCookie);
A cookie is created in the server in this way and it is added to the HttpServletResponse .
Then why is it that , a HttpServletRequest Header contain cookies ??
Cookies are transmitted per request, example use case:
- The first client request does not contain cookies
- Server sets cookie A, it is transmitted back to the client in HTTP header
Set-Cookie
; this is whatresponse.addCookie
does. - The second client request sends HTTP header
Cookie
containing a list of all matching cookies (including cookie A)
Cookies are part of the HTTP protocol. Wikipedia provides a fine overview: http://en.wikipedia.org/wiki/HTTP_cookie
精彩评论