aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why null is return? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why null is return?" Watch "Why null is return?" New topic
Author

Why null is return?

Grace Yang
Ranch Hand

Joined: Apr 01, 2007
Posts: 58
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

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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 ]

A good workman is known by his tools.
Tiffiny Yang
Ranch Hand

Joined: Mar 29, 2006
Posts: 124
interesting ,,,

then how to set the header and display the header "aaa" ?
Grace Yang
Ranch Hand

Joined: Apr 01, 2007
Posts: 58
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

Joined: Feb 05, 2003
Posts: 4727

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

Joined: Feb 28, 2007
Posts: 1707
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
Marshal

Joined: Mar 22, 2005
Posts: 41497
    
  53
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?


Ping & DNS - my free Android networking tools app
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
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

Joined: Feb 05, 2003
Posts: 4727

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

Joined: Jun 12, 2007
Posts: 21
you should set a Context attribute, so that the object is carried over all the calls.


SCJP 5.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41497
    
  53
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

Joined: Apr 20, 2006
Posts: 3344


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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why null is return?