Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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?
0
deltanins
Asked:
deltanins
2 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now