This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

response.setHeader not working

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you know that they're not being set?
Where are you trying to read these values?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet code is doing a forward and I dont see any redirect there. Where is the JSP?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could wrap your orginal request object. Read this article
http://dev2dev.bea.com/lpt/a/415
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually MyServlet takes the Header Info passes it on to other classes and thats it.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28304
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic