title: Java Servlet as a HTTP Proxy
body: <p>I have read hundreds of SO Posts and studied several Java HTTP-Proxy Sources available... but I could not find a solution for my Problem.</p>
  
  <p>I wrote a WebApp that proxies Http-Requests. The WebApp is working, but links and referrers become broken because the "Root" of the proxied page points to the root of my server and not to the path of my proxyservlet..</p>
  
  <p>To make it more clear:</p>
  
  <ol>
  <li><p>My ProxyServlet gets a Request "<a href="http://myserver.com/proxy/ProxyServlet?foo=bar" rel="nofollow">http://myserver.com/proxy/ProxyServlet?foo=bar</a>"</p></li>
  <li><p>The ProxyServlet now fetches the pagecontent from ServerX (e.g. "<a href="http://original.com/test.html" rel="nofollow">http://original.com/test.html</a>")</p></li>
  <li><p>The content of the page is delivered to the browser by just reading and writing from one stream to the other and copying the headers.</p></li>
  <li><p>The browser displays the page, the URL, that the browser shows is the original request ("<a href="http://myserver.com/proxy/ProxyServlet?foo=bar" rel="nofollow">http://myserver.com/proxy/ProxyServlet?foo=bar</a>"), but all relative links now point to
  "<a href="http://myserver.com/XXX.html" rel="nofollow">http://myserver.com/XXX.html</a>" instead of "<a href="http://myserver.com/proxy/ProxyServlet/XXX.html" rel="nofollow">http://myserver.com/proxy/ProxyServlet/XXX.html</a>"</p></li>
  </ol>
  
  <p>Is there a response-header where I can change the "path" so that relative links correctly point to my ProxyServlet?</p>
  
  <p>(Rewriting the page-content and replacing links would be too difficult, because the page contains relatively addressed elements such as  javascript code and other active content...)</p>
  
  <p>(Changing the mapping for my Servlet to "/*" is also not possible... it must be accessed via this path...)</p>