• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

response.addHeader question.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Pay attention! Tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic