File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes SetHeader Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "SetHeader Problem" Watch "SetHeader Problem" New topic

SetHeader Problem

Aaron Beverly

Joined: Sep 10, 2003
Posts: 19
I am attempting to set an http header and then read it back, like so:
response.setHeader("someHeader", "someHeaderValue");
response.setContentType("text/html; charset=" + this.getCharacterEncoding());
PrintWriter out = response.getWriter();
out.println("<br />");
When I compile and run, the response.containsHeader returns true, but request.getHeader always returns null.
I originally thought that this was related to the RequestDispatcher I am using in this same servlet, but after stripping the code down to what I wrote above, I still cannot get the header to set correctly.
I am using Tomcat 4.1.18; there is one Filter that the request is being passed through, but the header does not set even after I removed the filter (I had read that some versions of Tomcat display buggy behavior with regard to setHEader when filters are used).
Anyone have any idea why this is not working?
deepak yadav

Joined: Oct 04, 2001
Posts: 17
request and response are separate objects, if you are setting something in response u can't get it it out from request within same piece of code.
The reciever will get this response as request and would be able to get this thing out from the request object.
Aaron Beverly

Joined: Sep 10, 2003
Posts: 19
Hi, thanks for the response!
I did not correctly state my problem, I guess. The code I gave as an example is not actually in the same file. The request.getHeader("someHeader")' portion is in a separate servlet written simply to check for the existence of the http header I am trying to set. They are not run together. Sorry for the misstatement.
However, I still cannot seem to actually set the http header with the response.setHeader() method. The header never shows up. I tried running the same code on another installation of Tomcat and it DOES work there, so I am assuming there is something I am missing that is preventing the code from working correctly on the server it is currently on.
I agree. Here's the link:
subject: SetHeader Problem
It's not a secret anymore!