• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding headers

 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a problem in adding headers and then retrieving them. I try to add headers in the response in the following way:

response.setHeader("testheader", "Header1");

and then I try to add another value to the same header as :

response.addHeader("testheader", "Header2");

when I try to retrieve the header as :

String header = request.getHeader("testheader");

I get header as null.

Please tell me as to where I am possibly going wrong
 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken,

I think/guess:

Headers means the http's predefined headers.
e.g: Cache-Control,Pragma,Referer etc.

and you are adding your own header name.

Might be the problem is because of that.

Others(Experts),please correct if wrong.


Regards
Baseet Ahmed

Knowledge is not what memorised.
Knowledge is what give benefits.
(Imam Shafai)
 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again,

Also,
you have set the headers in response object and retrieving through request object. How come ?
This is not possible.



Regards
Baseet Ahmed

Knowledge is not what memorised.
Knowledge is what give benefits. (Imam Shafai)
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have set the headers in response object and retrieving through request object. This is not possible.

Indeed. Request headers are sent from the browser to the server; response headers are sent from the server to the browser. They are not related to one another.
 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so is there any way by which I would be able to retrieve the headers which I set in the response object?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Sampson:
ok, so is there any way by which I would be able to retrieve the headers which I set in the response object?


Only if you client will read them and add them to the next request.

Maybe if you tell us what you're trying to accomplish, someone can suggest an easier alternative.
 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

I am trying to understand exactly what you have written. Could you please tell me how I can add a customized header esp. the one which I showed above in the response object and obtain the same from the request object. How is it possible for the client to read the headers from the response object so that by invoking getHeader() on the request object I am able to retrieve the customized header.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, if you tell us what you're doing we might be able to suggest a better alternative. We know that you're trying to read and write headers, but to what end?

Ulf's last post sums up what headers do and why you can't read them back after setting them.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's like going to the mailbox, putting in some outgoing mail, picking up your incoming mail, and then being surprised that the letters you sent out are no longer in your hand.

Response headers and request headers have nothing to do with each other. If you're looking for a way to persist information across requests, headers are not what you should be considering.
[ April 03, 2008: Message edited by: Bear Bibeault ]
 
Ken Sampson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I got this... Thanks guys.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
It's like going to the mailbox, putting in some outgoing mail, picking up your incoming mail, and then being surprised that the letters you sent out are no longer in your hand.


Great analogy!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!