A URL is a unique address to an Internet resource. Here is a URL: http://www.java2s.com:80/index.htm
- HTTP: the protocol to use to retrieve the resource.
- http://www.yahoo.com/: the host, where the resource resides.
- 80 is the port number.
- /index.htm specifies the path of the URL.
In Java, a URL is represented by a java.net.URL object.
public URL (java.lang.String spec)
public URL (java.lang.String protocol, java.lang.String host,java.lang.String file)
public URL (java.lang.String protocol, java.lang.String host,int port, java.lang.String file)
public URL (URL context, String spec)
URL myUrl = new URL ("http://www.java2s.com/");
Because no page is specified, the default page is assumed.
The following lines of code create identical URL objects.
URL yahool = new URL ("http://www.yahoo.com/index.html");
URL yahoo2 = new URL ("http://www.yahoo.com", "/index.html");
URL yahoo3 = new URL ("http://www.yahoo.com", 80, "/index.html");