title: Getting server name the client know behind a reverse proxy within a Java EE web application
body: <p>For my Java EE web application I have a problem. I need to redirect a page A to another page B, using Spring controller I write:</p>
  
  <p>Page A is for my browser (in http):</p>
  
  <pre><code>      http://MYPUBLICSERVERNAME/MYCONTEXT/A.do :
  </code></pre>
  
  <p>In A which is a Spring controller has the following code for redirection:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET, value = "/A.do")
  public String searchProfiles(){
  ....
  return "redirect:/B.html";
  }
  </code></pre>
  
  <p>It works perfectly in http.
  so I go to B :</p>
  
  <pre><code>  http://MYPUBLICSERVERNAME/MYCONTEXT/B.html
  </code></pre>
  
  <p>The problem comes with HTTPS because with HTTPS a reverse proxy intercept the request :</p>
  
  <p>This proxy is on the same machine and I get :</p>
  
  <p>impossible to connect because when i go to :</p>
  
  <pre><code>httpS://MYPUBLICSERVERNAME/MYCONTEXT/A.do
  </code></pre>
  
  <p>i expected to be redirected to :</p>
  
  <pre><code>httpS://MYPUBLICSERVERNAME/MYCONTEXT/B.html
  </code></pre>
  
  <p>Instead the browser is redirected to </p>
  
  <pre><code> http://localhost/MYCONTEXT/B.html
  </code></pre>
  
  <p>I understand that for the server the client is the reverse proxy not of course the browser.</p>
  
  <p>So my question is : how can the servlet get the original server name i mean the server name indicated in the web browser (MYPUBLICSERVERNAME) and the protocol (HTTPS if HTTPS).</p>
  
  <p>The workaround i think is using a cookie feeded by the browser, because the browser only know MYPUBLICSERVERNAME but maybe more elegant solution exist ?</p>
  
  <p>Of course if I know a spring function that can magically redirect with expected protocol and server name it's ok too but i didn't find it.</p>
  
  <p>Thanks for your answer</p>