Previous | Next | Trail Map | Custom Networking and Security | Working with URLs


Parsing a URL

The URL class provides several methods that let you query URL objects. You can get the protocol, host name, port number, and filename from a URL using these accessor methods:
getProtocol()
Returns the protocol identifier component of the URL.
getHost()
Returns the host name component of the URL.
getPort()
Returns the port number component of the URL. The getPort() method returns an integer that is the port number. If the port is not set, getPort() returns -1.
getFile()
Returns the filename component of the URL.
getRef()
Returns the reference component of the URL.

Note: Remember that not all URL addresses contain these components. The URL class provides these methods because HTTP URLs do contain these components and are perhaps the most commonly used URLs. The URL class is somewhat HTTP-centric.

You can use these getXXX() methods to get information about the URL regardless of the constructor that you used to create the URL object.

The URL class, along with these accessor methods, frees you from ever having to parse URLs again! Given any string specification of a URL, just create a new URL object and call any of the accessor methods for the information you need. This small example program creates a URL from a string specification and then uses the URL object's accessor methods to parse the URL:

import java.net.*;
import java.io.*;

class ParseURL {
    public static void main(String[] args) {
        URL aURL = null;
        try {
            aURL = new URL("http://java.sun.com:80/tutorial/intro.html#DOWNLOADING");
            System.out.println("protocol = " + aURL.getProtocol());
            System.out.println("host = " + aURL.getHost());
            System.out.println("filename = " + aURL.getFile());
            System.out.println("port = " + aURL.getPort());
            System.out.println("ref = " + aURL.getRef());
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException: " + e);
        }
    }
}
Here's the output displayed by the program:
protocol = http
host = java.sun.com
filename = /tutorial/intro.html
port = 80
ref = DOWNLOADING

A Note About getRef()

In the JDK 1.0 release of Java, getRef() works only if you create the URL using one of these two constructors:
URL(String absoluteURLSpecification);
URL(URL baseURL, String relativeURLSpecification);

For example, suppose you create a URL with these statements:

URL gamelan = new URL("http://www.gamelan.com/");
URL gamelanNetworkBottom = new URL(gamelan, "Gamelan.network.html#BOTTOM");
The getRef() method, when invoked on gamelanNetworkBottom, correctly returns "BOTTOM". However, if you create a URL (referring to the same resource as previously) with this statement:
URL gamelanNetworkBottom = new URL("http", "www.gamelan.com", "Gamelan.network.html#BOTTOM");
The getRef() method, incorrectly, returns null.


Previous | Next | Trail Map | Custom Networking and Security | Working with URLs