?
Solved

Adding a cookie to a request

Posted on 2005-02-25
9
Medium Priority
?
232 Views
Last Modified: 2012-05-05
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
Comment
Question by:deltanins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13404899
You could use setAttribute() but this has warnings.
0
 
LVL 7

Expert Comment

by:lhankins
ID: 13405034
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
 
LVL 7

Expert Comment

by:lhankins
ID: 13405046
BTW, just for clarification wrt above snippet :

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


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:deltanins
ID: 13405215
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
 
LVL 7

Accepted Solution

by:
lhankins earned 600 total points
ID: 13406331
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
 

Author Comment

by:deltanins
ID: 13422001
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
 
LVL 28

Assisted Solution

by:rrz
rrz earned 150 total points
ID: 13422080
0
 

Author Comment

by:deltanins
ID: 13441919
thanks everyone.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question