| Working with URLs

## Creating a URL

> The easiest way to create a URL object is to create it from a String that represents the "human-readable" form of the URL address. This is typically the form that another person will use to tell you about a URL. For example, to tell you about the Gamelan site that contains a list of Java-capable sites we would give it to you in the following form:

> >     http://www.gamelan.com/

> In your Java program, you can use a string containing the above text to create a URL object:

> >     URL gamelan = new URL("http://www.gamelan.com/");

> The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question. You can also create URL objects from a relative URL address.

> #### Creating a URL Relative to Another

> > A relative URL contains only enough information to reach the resource relative to (or in the context of) another URL.

> > Relative URL specifications are often used within HTML files. For example, suppose you write an HTML file called `JoesHomePage.html`. Within this page, are links to other pages, `PicturesOfMe.html` and `MyKids.html`, that are on the same machine and in the same directory as `JoesHomePage.html`. The links to `PicturesOfMe.html` and `MyKids.html` from `JoesHomePage.html` could be specified just as filenames, like this:

> > >     <a href="PicturesOfMe.html">Pictures of Me</a>
> > >     <a href="MyKids.html">Pictures of My Kids</a>

> > These URL addresses are relative URLs. That is, the URLs are specified relative to the file in which they are contained--`JoesHomePage.html`.

> > In your Java programs, you can create a URL object from a relative URL specification. For example, suppose that you already created a URL for "http://www.gamelan.com/" in your program, and you know the names of several files at that site (`Gamelan.network.html`, and `Gamelan.animation.html`). You can create URLs for each file at the Gamelan site by simply specifying the filenames in the context of the original Gamelan URL. The filenames are relative URLs and are relative to the original Gamelan URL.

> > >     URL gamelan = new URL("http://www.gamelan.com/");
> > >     URL gamelanNetwork = new URL(gamelan, "Gamelan.network.html");

> > This code snippet uses the URL class constructor that lets you create a URL object from a URL object (the base) and a relative URL.

> > This constructor is also useful for creating URLs to named anchors (also known as references) within a file. For example, suppose the "Gamelan.network.html" file has a named anchor called `BOTTOM` that as at the bottom of the file. You can use the relative URL constructor to create a URL to it like this:

> > >     URL gamelanNetworkBottom = new URL(gamelanNetwork, "#BOTTOM");

> > The general form of this URL constructor is:

> > >     URL(URL baseURL, String relativeURL)

> > The first argument is a URL object that specifies the base of the new URL, and the second argument is a String that specifies the rest of the resource name relative to the base. If baseURL is null, then this constructor treats relativeURL as though it is an absolute URL specification. Conversely, if relativeURL is an absolute URL specification, then the constructor ignores baseURL.

> #### Other URL Constructors

> > The URL class provides two additional constructors for creating a URL object. These constructors are useful when you are working with URLs, such as HTTP URLs, that have host name, filename, port number, and reference components in the resource name portion of the URL. These two constructors are useful when you do not have a String containing the complete URL specification, but you do know various components of the URL.

> > For example, if you design a network browsing panel similar to a file browsing panel that let users use the mouse to choose the protocol, host name, port number, and filename, you can construct a URL from its components. The first constructor creates a URL from a protocol, host name, and filename. The following code snippet creates a URL to the `Gamelan.network.html` file at the Gamelan site:

> > >     URL gamelan = new URL("http", "www.gamelan.com", "/Gamelan.network.html");

> > This is equivalent to `URL("http://www.gamelan.com/Gamelan.network.html")`. The first argument is the protocol, the second argument is the host name, and the last argument is the pathname of the file. Note that the filename contains a slash (/) character at the beginning. This indicates that the filename is specified from the root of the host.

> > The final URL constructor adds the port number to the list of arguments used in the previous constructor.

> > >     URL gamelan = new URL("http", "www.gamelan.com", 80, "/Gamelan.network.html");

> > This creates a URL object for the following URL:

> > >     http://www.gamelan.com:80/Gamelan.network.html

> > If you construct a URL using one of these constructors, you can get a String containing the complete URL address using URL's `toString()` method or the equivalent `toExternalForm()` method.

> #### MalformedURLException

> > Each of the four URL constructors throws a MalformedURLException if the arguments to the constructor refer to a null or unknown protocol. Typically, you will want to catch and handle this exception. Thus you would normally embed your URL constructor statements in a try/catch pair.

> > >     try {
> > >         URL myURL = new URL(. . .)
> > >     } catch (MalformedURLException e) {
> > >         . . .
> > >         // exception handler code here
> > >         . . .
> > >     }

> > See Handling Errors Using Exceptions for information about handling exceptions.

> Note: URLs are "write-once" objects. Once you've created a URL object, you cannot change any of its attributes (protocol, host name, filename, or port number).

| Working with URLs