This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why null is return?

 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns me null when I run the snippet Jsp.
=============================
<%response.addHeader("abc", "123");%>

<%=request.getHeader("abc")%>
<br>

anybody can help?

Thanks.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With each request, all the prior header information is lost that is why,
you get null on calling request.getHeader(...)




Thanks,
[ July 13, 2007: Message edited by: Chandra Bhatt ]
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interesting ,,,

then how to set the header and display the header "aaa" ?
 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response back.

I tried set up header in a servelt and the forward to a JSP which I can not get the header value either.

I still don't know what's wrong?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bhatt:
My question is, how does the browser use the header information set by the server?

That depends on the header. Here are the response headers sent by the JavaRanch Saloon:

Can you think of a way in which the browser might use any of these?
 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your book has a "custom" example, then why doesn't your book explain how to use it? There might be a good reason for that. :roll:

My answer is:
You are trying to treat a header like attributes. If you need to set and display something, use an attribute.
 
Anil Chatty
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should set a Context attribute, so that the object is carried over all the calls.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are trying to treat a header like attributes. If you need to set and display something, use an attribute


I think this answer by Marc stands correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic