wood burning stoves 2.0*
The moose likes Servlets and the fly likes problem in using request.getHeader() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in using request.getHeader()" Watch "problem in using request.getHeader()" New topic
Author

problem in using request.getHeader()

Arun Prasath
Ranch Hand

Joined: Aug 14, 2006
Posts: 30
--------------------------------------------------------------------------------
i have some problem in using request.getHeader(String)

In a servlet my i set a header.
The code is:

response.setHeader("Arun","Prasath");


RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.forward(request,response);


Then i forward it to a jsp page ie,index.jsp


In the index.jsp i get the following result:

String ss=(String)request.getHeader("Arun");
out.println(ss);

The result i get while print the String is null.I expect the header value
prasath to be printed.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You're setting a header in the response and getting some header from the request. Two different places, so the header in the request being null is not surprising.

Please put some lowercase letters in your name Thank you.


[My Blog]
All roads lead to JavaRanch
Arun Prasath
Ranch Hand

Joined: Aug 14, 2006
Posts: 30
see iam forwarding the response from the servlet to another jsp page.so
that response object should hold the header "Arun" surely coz i have not thrown my reponse to the browser,I have forwarded it to the jsp.

Then why i get a null value.How it got missed
Arun Prasath
Ranch Hand

Joined: Aug 14, 2006
Posts: 30
See we can set header only in response object.
ie,response.setHeader("arun","prasath");

we canretrieve the header value only using
request.getHeader("arun");
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Don't use headers for this, use attributes.
request.setAttribute and request.getAttribute.

Please don't forget to change your name
Thank you.
Arun Prasath
Ranch Hand

Joined: Aug 14, 2006
Posts: 30
see Iam using this for getting the lanid which i need to set on the Header for each users...

if i cant do this custom Header means,why they have given that
response.setHeader(String,String);

I know setAttribute but i dont want that....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41101
    
  45
I know setAttribute but i dont want that....

Why not? It's the right way to do it, since -as you have found out- using request and response headers doesn't work.


Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

HTTP response headers allow the server to send meta data about the response from the server to the client. There is nothing that would cause the browser to return your custom headers (unles you've built your own client).

Furthermore, even if there was, in your case, you're trying to read them before the response has ever gone to the client (forwards don't involve a trip down to the client).

As suggested earlier, request attributes, are what you would use to pass values between components within a request.


And, as Satou has suggested, you would be doing all of our eyes a favor if you fixed the casing in your name. You can do it here.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in using request.getHeader()
 
Similar Threads
problem in request.getHeader()
Problem using setHeader() method
Adding headers
one more doubt
Header Variables