difference between response.addCookie and response.setHeader

You can also choose to add cookies via response.setHeader(""Set-Cookie"....)
Why would you want to add a cookie using response.setHeader over response.addCookie? Are there any important differences of doing one over the other?

Thanks In advance
Who is Participating?
sandeshjConnect With a Mentor Commented:
Oh yes you're right..
You can use setHeader too, although not advisable because you have a more specialized api to do the same.

Specialised because cookie header is complicated...Refer:  http://msdn.microsoft.com/en-us/library/aa384321(VS.85).aspx

In order to make use of all these features of a cookie, you can set a cookie object and then use addCookie api appropriately.
setHeader method of response object is used to create an HTTP Header with the name and value given as string.

addCookie() method of response object is used to add the specified cookie to the response.
response.addCookie(Cookie yourSystem);

Both are different. You can't set a cookie using setHeader. Its for passing plain Name-Value Pairs in the Header of the response.
singh101Author Commented:
I am under the assumption you can set a cookie using response.setHeader("Set-Cookie.....)
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CEHJConnect With a Mentor Commented:
The main difference is that a cookie has the ability to be persistent on the client machine

objectsConnect With a Mentor Commented:
They both do the same thing
addCookie() just handles the formatting of the header for you

You should use addCookie() to avoid errors that can be made with setHeader()

> The main difference is that a cookie has the ability to be persistent on the client machine

Thats incorrect btw, theres no difference
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.