A URL is a unique address to an Internet resource. For example, every page on the Internet has a different URL. Here is a URL:
http://www.yahoo.com:80/en/index.html
A URL has several
components. The first component denotes the protocol to use to retrieve
the resource. In the preceding example, the protocol is HTTP. The second
part, www.yahoo.com,
is the host. It tells you where the resource resides. Number 80 after
the host is the port number. The last part, /en/index.html, specifies
the path of the URL. By default, the HTTP uses port 80.
The HTTP is the most common
protocol used in a URL. However, it is not the only one. For example,
this URL refers to a jpeg file in the local computer.
file://localhost/C:/data/MyPhoto.jpg
Detailed information about URLs can be found at this location:
http://www.ietf.org/rfc/rfc2396.txt
In Java, a URL is represented by a java.net.URL object. You construct a URL by invoking one of the URL class's constructors. Here are some easier constructors:
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)
Here is an example.
URL myUrl = new URL("http://www.brainysoftware.com/");
Because no page is specified, the default page is assumed.
As another example, the following lines of code create identical URL objects.
URL yahoo1 = 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");
Parsing a URL
You can retrieve the various components of a URL object by using these methods:
public java.lang.String getFile()
public java.lang.String getHost()
public java.lang.String getPath()
public int getPort()
public java.lang.String getProtocol()
public java.lang.String getQuery()
For example, the code in Listing 1 creates a URL and prints its various parts.
Listing 1. Parsing a URL
package app21;
import java.net.URL;
public class URLTest1 {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://www.yahoo.com:80/en/index.html?name=john#first");
System.out.println("protocol:" + url.getProtocol());
System.out.println("port:" + url.getPort());
System.out.println("host:" + url.getHost());
System.out.println("path:" + url.getPath());
System.out.println("file:" + url.getFile());
System.out.println("query:" + url.getQuery());
System.out.println("ref:" + url.getRef());
}
}
|
The result of running the URLTest1 class is as follows.
protocol:http
port:80
host:www.yahoo.com
path:/en/index.html
file:/en/index.html?name=john
query:name=john
ref:first
Reading A Web Resource
You can use the URL class's openStream method to read a Web resource. Here is the signature of this method.
public final java.io.InputStream openStream()
throws java.io.IOException
For example, the URLTest2 class in Listing 2 prints the content of http://www.google.com.
Listing 2. Opening a URL's stream
package app21;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest2 {
public static void main(String[] args) {
try {
URL url = new URL("http://www.google.com/");
InputStream inputStream = url.openStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
while (line!= null) {
System.out.println(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
|
NOTE
You can use a URL only to read a Web resource. To write to a server, use a java.net.URLConnection object.