10

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].

Here is some code I have used, but don't know if this is the best approach. (this is pseudo code)

HttpServletRequest == request

String serverName = request.getServerName().toLowerCase();
String scheme = request.getScheme();
int port = request.getServerPort();

String val = scheme + "://" + serverName + ":" port;

Such that val returns:

http(s)://server.com/

or

http(s)://server.com:7770

Basically, I need everything but the query string and 'context'.

I was also consider using URL:

String absURL = request.getRequestURL();
URL url = new URL(absURL);

url.get????
dfa
  • 114,442
  • 31
  • 189
  • 228
Berlin Brown
  • 11,504
  • 37
  • 135
  • 203

6 Answers6

19

try this:

URL serverURL = new URL(request.getScheme(),      // http
                        request.getServerName(),  // host
                        request.getServerPort(),  // port
                        "");                      // file

EDIT

hiding default port on http and https:

int port = request.getServerPort();

if (request.getScheme().equals("http") && port == 80) {
    port = -1;
} else if (request.getScheme().equals("https") && port == 443) {
    port = -1;
}

URL serverURL = new URL(request.getScheme(), request.getServerName(), port, "");
dfa
  • 114,442
  • 31
  • 189
  • 228
2
URI u=new URI("http://www.google.com/");
String s=u.getScheme()+"://"+u.getHost()+":"+u.getPort();

As Cookie said, from java.net.URI (docs).

mcandre
  • 22,868
  • 20
  • 88
  • 147
1
public String getServer(HttpServletRequest request) {
  int port = request.getServerPort();
  StringBuilder result = new StringBuilder();
  result.append(request.getScheme())
        .append("://")
        .append(request.getServerName());

  if (port != 80) {
    result.append(':')
          .append(port);
  }

  return result;
}
John Topley
  • 113,588
  • 46
  • 195
  • 237
0

I think java.net.URI does what you want.

CookieOfFortune
  • 13,836
  • 8
  • 42
  • 58
0

If you want to preserve the URL as it appeared in the request (e.g. leaving off the port if it wasn't explicitly given), you can use something like this. The regex matches HTTP and HTTPS URLs. Capture group 1 contains the server root from the scheme to the optional port. (That's the one you want.) Group 2 contains the host name only.

String regex = "(^http[s]?://([\\w\\-_]+(?:\\.[\\w\\-_]+)*)(?:\\:[\\d]+)?).*$";
Matcher urlMatcher = Pattern.compile(regex).matcher(request.getRequestURL());
String serverRoot = urlMatcher.group(1);
Rob H
  • 14,502
  • 8
  • 42
  • 45
0

Instead of using the StringBuilder, or concatenating parts of the URL, use the org.springframework.web.servlet.support.ServletUriComponentsBuilder class instead to form the URL. You could do something like

URL url = ServletUriComponentsBuilder.fromRequestUri(req).
                replacePath("/").build()
                .toUri().toURL();

Please find this article explaining several ways to use this class effectively.

Krishna Vedula
  • 1,643
  • 1
  • 27
  • 31