code: public static String getAppURL(HttpServletRequest request) {
  		StringBuffer url = new StringBuffer();
  		int port = request.getServerPort();
  		if (port < 0) {
  			port = 80; // Work around java.net.URL bug
  		}
  		String scheme = request.getScheme();
  		url.append(scheme);
  		url.append("://");
  		url.append(request.getServerName());
  		if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
  			url.append(':');
  			url.append(port);
  		}
  		return url.toString();
  	}
docs: Convenience method to get the application's URL based on request
  variables.