Resolve a relative URL in Java

Description

The following code shows how to resolve a relative URL.

Example


//from  w w w .  j av a 2  s .  c  om
import java.net.URL;

public class Main {
  public static void main(String[] argv) throws Exception {
    URL relativeURL, baseURL;
    baseURL = new URL("http://www.yourserver.com/");
    relativeURL = new URL(baseURL, "./a.htm");
    System.out.println(relativeURL.toExternalForm());

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode