Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in using request.getHeader()

 
Arun Prasath
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Prasath
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic