aspose file tools*
The moose likes Servlets and the fly likes response.setHeader not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "response.setHeader not working" Watch "response.setHeader not working" New topic
Author

response.setHeader not working

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi All,

I am facing a problem with response.setHeader method.

I am invoking that method in a servlet and then redirecting the response to another servlet. Now when in the other servlet I do request.getHeader I am unable to retrieve the value. I am using Tomcat 5.

Here is the complete code that is setting the header.




Complete code for reading the header.



Is there something I am missing?
[ January 31, 2006: Message edited by: Anupam Sinha ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How do you know that they're not being set?
Where are you trying to read these values?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
vijay gubbala
Greenhorn

Joined: Jan 31, 2006
Posts: 1
I think you are getting writer object before you are setting header. so it might not be working. headers are to be set before getting writer obj
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Thanks for your help.

Sorry for not providing the code.

Here is the complete code that is setting the header.




Complete code for reading the header.



I have also edited my first post to include the same.
[ January 31, 2006: Message edited by: Anupam Sinha ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

I dont think if you do a "forward", a response.header value will be available in the request object of the forwarded servlet. The request and response object do not change and the same objects will be available in the forwarded servlet/JSP as it is not a a "new" request but a "forwarded" request.


Groovy
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
A response header is something completely different than a request header. Specifically, the first servlets response headers are not the seconds servlets request headers. If you want to pass along data, you can use the request.setAttribute/getAttribute methods.


Ping & DNS - my free Android networking tools app
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The servlet code is doing a forward and I dont see any redirect there. Where is the JSP?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you haven't already, I recommend that you download and install the LiveHttpHeaders plugin for FireFox. With it, you can watch both the request and response headers in real time. It's a great tool for learning about the HTTP protocol.

Once you do, you will be able to see that response headers go from the server to the browser and request headers go from the browser to the server.
You don't read response headers from a servlet.
If you want to pass objects bewtween various server components, do as Ulf recommended and use the get/getAttribute methods of the request, session, or context objects.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Actually this was just a test code. I don't require a JSP but in case it can solve the problem please tell me about it.

Secondly I want to set the headers. I was unable to find a request.setHeader(). I thought we set the header in the response and read it from the request. If that isn't the case please enlighten.

My actual requirement is to test a bunch of classes who kick in after they recieve a request. The parameters are set in the request whereas some some data need to be collected from the header. I cannot use getParameter as I would be recieving some info in the header only. Is there any way wherein I can set the header in the request.

If I understand Ben correctly you mean to say that I would need to commit the response and then only I would be able to get those headers in the request. So if thats the case as Pradip stated sandwiching a JSP between them should solve the problem. The JSP would read the headers and set them as Attributes and then the servlet can later work on the attributes.
[ January 31, 2006: Message edited by: Anupam Sinha ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

I also could not understand the meaning of response.setHeader.

The response headers set by the Servlet will be available(after the request is committed) to the client(which could be your browser) which originally made the request.
By "forwarding" your request you are just making another class(servlet/JSP)to also handle the "same" request.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

You could wrap your orginal request object. Read this article
http://dev2dev.bea.com/lpt/a/415
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Thanks all of you for your support.

Actually I tried redirecting to a JSP as well. In that case as well the Header value is not available.

Code for setting the header.


JSP code for reading the header.


Meanwhile I am also downloading the Firefox plugin.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Anupam,

The above code posted by you wont work. Please check the Wrapper example in the link that I have posted earlier or use request.setAttribute(). The former seems to be the solution for your case.

You will have to override the getheader method of HttpServletRequestWrapper and return the values as required. Pass instance of the WrapperSubclass in the rd.forward(newWrapper,response).
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I downloaded the LiveHttpHeader for Mozilla. It's great!

Now I can see that my header is there. But I am not able to access the same.

Any ideas about it. Pasted below is the output from LiveHttpHeader plugin from Mozilla.

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The header set in the response object will only be available to the client i.e. browser and the it wont be accessible to any servlet/JSP at the server side. The response for client's consumption and not server's. You can only check whether a reponse header has already been set using response.containsHeadercontainsHeader(java.lang.String name).

Returns a boolean indicating whether the named response header has already been set.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Pradip, please clarify where should I overide the getHeader method. I mean should I do that in my servlet and JSP.

I am going thorugh your link. Hoping it solves my problem.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The link will solve your problem. You need to write a class similar to MyRequestWrapper and overide the getHeader(string name) method. In this method you will write code:




Create an instance of this class and pass it in the forward method.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Pradip, That was indeed quite helpful. But the present scenario is like this.

1. Client request a page from servlet MyServlet.
2. MyServlet gets the Name from the client's headers.
3. MyServlet processes the request.

Now the problem is that I cannot change the client's behaviour. The page that the client sends would contain the info that may change. So to replicate the behaviour I would like to create a page with headers set in.

Secondly overiding the method would mean hardcoding the value which again would be a problem. Is there any way I can set my actual headers and read it also.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

1. Client request a page from servlet MyServlet.
2. MyServlet gets the Name from the client's headers.
3. MyServlet processes the request.


What happens next ? The 4th step ?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Actually MyServlet takes the Header Info passes it on to other classes and thats it.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Anupam Sinha:
Actually MyServlet takes the Header Info passes it on to other classes and thats it.


Well in that case the header info is still availble in the request object ?Can't your other classes read from the request.getHeader()?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Actually the problem is I am not able to set the header. Reading them is fine. If I overide the getHeader I won't be able to change the name to something else otherthan what has been hardcoded there. i.e I won't be able respond to different requests differently.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Anupam Sinha:
Actually this was just a test code. I don't require a JSP but in case it can solve the problem please tell me about it.

Secondly I want to set the headers. I was unable to find a request.setHeader(). I thought we set the header in the response and read it from the request. If that isn't the case please enlighten.

My actual requirement is to test a bunch of classes who kick in after they recieve a request. The parameters are set in the request whereas some some data need to be collected from the header. I cannot use getParameter as I would be recieving some info in the header only. Is there any way wherein I can set the header in the request.

If I understand Ben correctly you mean to say that I would need to commit the response and then only I would be able to get those headers in the request. So if thats the case as Pradip stated sandwiching a JSP between them should solve the problem. The JSP would read the headers and set them as Attributes and then the servlet can later work on the attributes.

[ January 31, 2006: Message edited by: Anupam Sinha ]



I'm getting a sense that you don't have a good grasp on the purpose of headers.

Think of them as a way for the server and browser to send messages to one another. Unless you have a way to make the browser set the headers for you when making requests, you're not going to be able to read them from the server.

Try explaining exactly what you want to accomplish.
Maybe someone will have an easier way to do it.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi All,

We have developed a code wherein the client request would contain a few parameters and would also contain some headers.

Now that application is quite complete. As the client requests would contain info in the header as well, we had written code for retrieving the same. Now we have to check the whole application as a whole. To check the application we need to set the header so that we can replicate it as a request from the client. But I am not able to do the same. I need to set the header info as if the client had set it and process it as if processing a request from the client.

In case there's any more clarification needed please post.

Thanks to all of you for your help.

Atleast now Ben I think with the help of all the fellow javaranchers have a pretty good idea about what headers are.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

If you want to add some information to the request, for whatever reason, why not just add attributes to it? That's what attributes are for. You can add them, retrieve them, and even remove them in a totally straightforward way.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you want to simulate a request, why not build a mock client with either jakarta commons httpClient or with java.net.URLConnection?

Then you can add headers, or parameters.

Response headers set at the server level won't necessarily be sent back in the next request from the browser so I don't see how you could test this otherwise.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Exactly Ben that was what I needed. I am free to use any API. I am presently trying out Jakarta Cactus.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Thanks all of you for your support. I managed to set the header for creating the test enviorment. This is for infomation in case anyone gets stuck with a similar problem.

I used Jakarta Cactus. Using Cactus you can set the Headers, Parameters, Attributes before before calling the test class.

Again thanks to all of you for your support.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.setHeader not working