• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Why null is return?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should set a Context attribute, so that the object is carried over all the calls.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic