Adding a cookie to a request

Is there any way to add a cookie to a request? I have an intermediate stage (server) in between the users request and the server that processes the request and forms a response. At this intermediate stage I want to add more cookies onto the request before forwarding the request to its final destination, but as far as I've seen HttpServletRequest doesn't have an addCookie() method. Has anyone found a way to get around this?
deltaninsAsked:
Who is Participating?
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.

bloodredsunCommented:
You could use setAttribute() but this has warnings.
0
lhankinsCommented:
Just create the cookie and add it to the HttpServletResponse :

      Cookie cookie = new Cookie("MyCookieName", "somevalue");

      //--- default expiration to 1 day...
      cookie.setMaxAge(60*60*24);
      cookie.setPath("");

     //--- you could also set the cookie domain if you want...
     // cookie.setDomain("yahoo.com");

     httpResponse.addCookie(cookie);
0
lhankinsCommented:
BTW, just for clarification wrt above snippet :

   cookie is a javax.servlet.http.Cookie
   httpResponse is a javax.servlet.http.HttpServletResponse


0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

deltaninsAuthor Commented:
I don't want to set the cookie to the response, because I want it added to the request that goes to the internal server, not the response that goes back to the user.

Investigating setAttribute().....
0
lhankinsCommented:
Cookies are transmitted in a header in the HttpRequest, I doubt you can achieve what you want by adding to the attributes...    You might be able to add to the existing cookie header for the HttpRequest, but I would imagine this is prohibited.  

What is it you're trying to do..?   Does some code downstream from you need the cookie to be in the request...?     Is this Java code...?   If so, you might be able to wrap the existing HttpServletRequest object with your own implementation of the HttpServletRequest interface (delegates to underlying object except in the case of the getCookies() method, in which case it adds the additional Cookie before returning).
0

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
deltaninsAuthor Commented:
Yes, I've code downstream that needs the cookie to be in the request, and it's all Java code.
How would I write my own code to add the cookie to the request? I've tried to find the method details for addCookie when it's added to a response, but can't find the source code anywhere. Is it a case of adding/amending the header on the request?
0
rrzCommented:
0
deltaninsAuthor Commented:
thanks everyone.
0
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

From novice to tech pro — start learning today.

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.