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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.....)
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

Mick BarryJava DeveloperCommented:
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()

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

Thats incorrect btw, theres no difference
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java App Servers

From novice to tech pro — start learning today.