title: Java: String representation of just the host, scheme, possibly port from servlet request
body: <p>I work with different servers and configurations.  What is the best java code approach for getting the scheme://host:[port if it is not port 80].</p>
  
  <p>Here is some code I have used, but don't know if this is the best approach.
  (this is pseudo code)</p>
  
  <p>HttpServletRequest == request</p>
  
  <pre><code>String serverName = request.getServerName().toLowerCase();
  String scheme = request.getScheme();
  int port = request.getServerPort();
  
  String val = scheme + "://" + serverName + ":" port;
  </code></pre>
  
  <p>Such that val returns: </p>
  
  <pre>http(s)://server.com/
  </pre>
  
  <p>or    </p>
  
  <pre>http(s)://server.com:7770
  </pre>
  
  <p>Basically, I need everything but the query string and 'context'.</p>
  
  <p>I was also consider using URL:</p>
  
  <pre><code>String absURL = request.getRequestURL();
  URL url = new URL(absURL);
  
  url.get????
  </code></pre>