Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response.addHeader question.

 
Anna Wang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everybody:

I run this servlet code, I expect it will output "head3", to my suprise, it prints null, can anybody explain it? Thanks a lot.

public doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
response.addHeader("a", "head1");
response.addHeader("a", "head2");
response.setHeader("a", "head3");
String s = request.getHeader("a");
out.println(s);
}



later, I tried this code, the output is also null.
...
{
...
response.addHeader("a", "head1");
String s = request.getHeader("a");
out.println(s);
}
Please explain it. Thanks.
 
cheenu Dev
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey..set the header before getting the writer.

and for the second case..hehe he..(joking)
you wont get..why?
think guy..you set header for output (response) which goes back to the client(in generic). request is from client.
request is got from client..and response is sent to client.
you cannot mix both.
hope this is clear.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anna,
after setting the header in response you redirect it to another page
lets say pageB.jsp and then you get the the header from pageB.jsp from request object.



Shivani
[ August 31, 2006: Message edited by: Shivani Chandna ]
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivani,

I didn't get your answer . How come we get the response headers if we redirect to another jsp .

As far as my understanding goes , there is no way to retrieve response parameters from request object , no matter whether we redirect or forward.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Vivek. The rest is unclear
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek,

Right you are!... . Using cookies related methods is the way to communicate.

Forwarding and redirecting just uses the existing request/response object.
The only thing that can be done for getting modified headers is to use a wrapper over the existing HttpServletRequest object and overriding the getHeader() method.

That is unless we are using an interceptor and modifying the headers.
[ September 01, 2006: Message edited by: Shivani Chandna ]
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a small modification ..

Forwarding will use the same request/response object but redirecting creates a fresh set of request / response objects
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic