aspose file tools*
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
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "response.addHeader question." Watch "response.addHeader question." New topic
Author

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");
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

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 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.


cheenujunk@gmail.com
Shivani Chandna
Ranch Hand

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

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

Joined: Mar 27, 2006
Posts: 245
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.


Vikrant Pandit
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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
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

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: http://aspose.com/file-tools
 
subject: response.addHeader question.