You are adding a header to the response object and trying to read a header from the request object. These are two different objects, so why would you expect anything different? [ July 13, 2007: Message edited by: Marc Peabody ]
You are trying to treat a header like attributes. If you need to set and display something, use an attribute.
But now an explanation why this is "not working".
A request is something you send from your browser. The response is what it expects to get back. The headers in the request are determined by your browser when it sends the request to the server. Those headers cannot change. Actually, not much on the request object can change. Why? The browser is sending a big, long, nasty chunk of text to the server. The request (HttpServletRequest) object in J2EE is merely an abstraction of that nasty text. It deals with the ugliness of parsing out that text and provides a clean Java class interface instead to make your life easier.
The headers on the response are only to be set to communicate something to the browser. For instance, a header can be used to tell the browser not to cache the response. Similarly, the response object is an abstraction to the big, long, nasty chunk of text that the server is going to return to the browser. Most of the things you alter on the response object are meant for the browser, not for the developer.
That's very nice Marc. My question is, how does the browser use the header information set by the server? As we do response.addHeader(...) and where does the browser use that header information. I would expect any example on this.
my question is : If headers are to be communicated only with the browser then why do the examples in many of the books are for a custom header which is not a standard one. And how can we actually use a custom header?
Originally posted by nitin pai: If headers are to be communicated only with the browser then why do the examples in many of the books are for a custom header which is not a standard one. And how can we actually use a custom header?
Headers are sent to the HTTP client, which is not necessarily a browser. If someone wrote a program that autonomically accesses a web site, custom headers could be used to transfer additional information between client and server.
E.g., the SOAP HTTP binding and the WebDAV HTTP extension use additional headers, which a browser wouldn't understand.
But as Marc said, HTTP headers and request attributes are quite different things, even if their API is similar.