This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes response.addHeader question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "response.addHeader question." Watch "response.addHeader question." New topic

response.addHeader question.

Anna Wang
Ranch Hand

Joined: May 26, 2006
Posts: 30
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");

later, I tried this code, the output is also null.
response.addHeader("a", "head1");
String s = request.getHeader("a");
Please explain it. Thanks.
cheenu Dev
Ranch Hand

Joined: Nov 13, 2005
Posts: 276
hey..set the header before getting the writer.

and for the second case..hehe he..(joking)
you wont get..why?
think 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

Joined: Sep 18, 2004
Posts: 380
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.

[ August 31, 2006: Message edited by: Shivani Chandna ]

/** Code speaks louder than words */
Vikrant Pandit
Ranch Hand

Joined: Mar 27, 2006
Posts: 245

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.

Vikrant Pandit
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

I agree with Vivek. The rest is unclear

[My Blog]
All roads lead to JavaRanch
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380

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

Joined: Mar 27, 2006
Posts: 245
Just a small modification ..

Forwarding will use the same request/response object but redirecting creates a fresh set of request / response objects
I agree. Here's the link:
subject: response.addHeader question.
Similar Threads
Please Help! recursive call
Help DataTable
Constructor Problem
setting http Header using urlconnection