• Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpServletResponse.addCookie same as HttpServletResponse.setHeader ??  RSS feed

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wanted to know if adding a Cookie and sending Headers has same effect ??? I came across this thing in the apidocs


Source: java.lang.Cookie

Cookies are small amounts of information, which are sent by the Servlet to the Web Browser, saved by the Browser, and later sent back to the Server. The Servlet sends cookies to the browser by using the HttpServletResponse.addCookie(Cookie) method, which adds fields to HTTP response headers to send cookies to the bro

wser, one at a time.

Based on this I am assuming that they have the same effect. No matter if you send info as cookie or thru response headers, all of it will end up as cookie on the client side. Is my understanding correct here??
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the HttpServletRequest.getCookies() to verify your claim?
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If adding cookies was a simple as adding a header then there would be no need for the Cookie class and all that it does. While it's ultimately correct that cookies are transferred as headers, there's a lot more to it than it may seem at first.
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:If adding cookies was a simple as adding a header then there would be no need for the Cookie class and all that it does. While it's ultimately correct that cookies are transferred as headers, there's a lot more to it than it may seem at first.



Thanks for this explanation. I was confused mainly because in one of the whizlabs mock test.


A web application prefers to use cookies for storing the user name. Identify all the possible methods, which can be used to set the cookies. Select two choices.
Choose at least one answer.
a. HttpServletResponse.setCookie(String name, String value)
b. HttpServletResponse.addCookie(Cookie aCookieObject)
c. HttpServletResponse.setCookie(Cookie aCookieObject)
d. HttpServletResponse.addCookie(setCookie(String name, String value)
e. HttpServletResponse.setHeader(String name, String value)

Choices B and E are the correct answers.



B is obvious but for explanation of E above portion was quoted from the api's. I thought of clarifying the portion itself.
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamal you can send cookies as headers but the syntax of cookie header is complicated. You can see the format here...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!