title: getting URL to a webapp context (the base URL)
body: <p>Sometimes you need to construct a full URL to your web app context inside a servlet/JSP/whatever based on <code>HttpServletRequest</code>.
  Something like <strong>http://server.name:8080/context/</strong>. Servlet API doesn't have a single method to achieve this.</p>
  
  <p>The straightforward approach is to append all URL components to a <code>StringBuffer</code>, like </p>
  
  <pre><code>ctxUrl = sb.append(req.getScheme()).append("://")
  .append(req.getgetServerName()).append(":")
  .append(req.getServerPort()) etc
  </code></pre>
  
  <p>I wonder if there's anything wrong with this alternative (which is 10 times faster):</p>
  
  <pre><code>ctxUrl = req.getRequestURL();
  ctxUrl = ctxUrl.substring(0, ctxUrl.lastIndexOf("/")));
  </code></pre>
  
  <p>Will two above methods <em>always</em> produce the same result?</p>