How to pass data in HTTP Header while redirecting a request in Java

anij picture anij · Jun 12, 2014 · Viewed 59k times · Source

is it possible to pass some data in HTTP Header, while redirecting a request from one server to another.

Here is my scenario, I have one generic filter, via which every request is passing. Now, based on some condition, I'm redirecting the request to some different server using the API objHttpServletResponse.sendRedirect(strURL).

But, the issue is, when I'm setting some data in response header like objHttpServletResponse.setHeader("Key", "Value"); That's not available in the redirected server.

So, my questions are,

1. Is there any way to pass some data in header while redirecting a request?

2. If not, what are the other possible ways to send some data while redirecting a request?

Please Note: few other ways, like

using URL parameters: objHttpServletResponse.sendRedirect(strURL+"?param="+ strParamValue);

or

using session: HttpSession session = httpRequest.getSession(); session.setAttribute("Key", "Value");

is not what I'm expecting.

Answer

flup picture flup · Jun 19, 2014

The headers you set are written to the response that gets sent to the client, along with a Location header and a status code. See Redirecting a request using servlets and the "setHeader" method not working

The client is then supposed to send an identical request to the URL you specified in the Location header. Identical to the request it sent to you.

You want the browser to send a header you specify along with the redirected request. Have you considered adding a (domain) Cookie header? Some googling leads me to believe that cookies set in a redirect response will get picked up by most browsers. See http://blog.dubbelboer.com/2012/11/25/302-cookie.html