code: private String getUrlPrefix( HttpServletRequest req )
      {
          StringBuilder url = new StringBuilder();
          String scheme = req.getScheme();
          int port = req.getServerPort();
          url.append( scheme );        // http, https
          url.append( "://" );
          url.append( req.getServerName() );
          if ( ( scheme.equals( "http" ) && port != 80 ) || ( scheme.equals( "https" ) && port != 443 ) )
          {
              url.append( ':' );
              url.append( req.getServerPort() );
          }
          return url.toString();
      }
docs: This code is heavily inspired by the stuff in HttpUtils.getRequestURL