Issue
I have a simple question. I would expect that
httpServletResponse.setHeader(“Location”, httpServletRequest.getRequestURL().toString())
would return to itself. But within our clustered infrastructure with IIS and multiple TOMCAT's
httpServletRequest.getRequestURL()
points to IIS (clustermanager). Is this correct behaviour, because I'm not sure? The redirect fails at the moment. I do understand that I could use a relative address (and that works), but I just would like to know if the observed behaviour (redirect fails) is as expected?
Solution
A redirect sends a response to the client (browser), effectively asking the browser to send a completely new request.
The consequence of that is that the route will be followed from scratch. If there are reverse proxies and load balancers in front of the application server, then there are chances of the redirect going to a different instance of the application (a different cluster member).
To send the request to a different resource in the same application on the same server, use forward
instead of sending a redirect:
httpServletRequest.getRequestDispatcher(httpServletRequest.getContextPath())
.forward(request, response);
The above code will theoretically send the request to the same servlet instance.
The redirect fails at the moment.
There are many possible reasons for this to fail. The most likely is that your reverse proxy (IIS) is using local network addresses to talk to your application server (such as an internal IP address in the Host
header, etc.). You may need to redirect to the host name as originally used by the browser (if domain name is known, or you could make your reverse proxy forward the Host
header somehow). Some reverse proxies/web servers support rewriting.
Answered By - ernest_k