Java URL(URL context, String spec) Constructor

Syntax

URL(URL context, String spec) constructor from URL has the following syntax.

public URL(URL context,  String spec) throws MalformedURLException

Example

In the following code shows how to use URL.URL(URL context, String spec) constructor.


import java.net.MalformedURLException;
import java.net.URL;
/*from w w w .j  a v a 2 s  . c o  m*/
public class Main{

  public static void main(String[] a) {

    try {
      URL base = new URL("http://www.java2s.com/");
      URL relative = new URL(base, "index.html");
      System.out.println(relative);
    } catch (MalformedURLException ex) {
      ;
    }

  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.net »




CookieManager
CookiePolicy
CookieStore
DatagramPacket
DatagramSocket
HttpCookie
HttpURLConnection
InetAddress
JarURLConnection
MulticastSocket
ServerSocket
Socket
SocketAddress
URI
URL
URLConnection
URLDecoder
URLEncoder