开发者

A query about Cookies in Servlets

开发者 https://www.devze.com 2023-03-19 19:52 出处:网络
Cookie usernameCookie = new Cookie (\"username\", attributeUsername); usernameCookie.setMaxAge(24*60*60);开发者_开发知识库
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:

  1. The first client request does not contain cookies
  2. Server sets cookie A, it is transmitted back to the client in HTTP header Set-Cookie; this is what response.addCookie does.
  3. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消