Working with URLs |
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, if I were to tell you about the Gamelan site that contains a list of Java-capable sites I would give it to you in the following form:In your Java program, you can use a string containing the above text to create a URL object: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 URLs from a relative URL address.URL gamelan = new URL("http://www.gamelan.com/");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 had written an HTML file, called
JoesHomePage.html
. Within this page, you had links to other pages,PicturesOfMe.html
andMyKids.html
, that were located in on the same machine and in the same directory asJoesHomePage.html
. The links toPicturesOfMe.html
andMyKids.html
fromJoesHomePage.html
could be specified just as filesnames, like this:These URL addresses are relative URLs. That is, the URLs are specified relative to the file in which they are contained--<a href="PicturesOfMe.html">Pictures of Me</a> <a href="MyKids.html">Pictures of My Kids</a>JoesHomePage.html
.In your Java programs, you can create a URL object from a relative URL specification. For example, suppose that you had already created a URL for "http://www.gamelan.com/" in your program, and you knew the names of several files at that site (
Gamelan.network.html
, andGamelan.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.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.URL gamelan = new URL("http://www.gamelan.com/"); URL gamelanNetwork = new URL(gamelan, "Gamelan.network.html");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 had a named anchor called
BOTTOM
that was anchored to the bottom of the file. You can use the relative URL constructor to create a URL to it like this:The general form of this URL constructor is:URL gamelan = new URL("http://www.gamelan.com/Gamelan.network.html"); URL gamelanNetworkBottom = new URL(gamelan, "#BOTTOM");where 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 were an absolute URL specification.URL(URL baseURL, String relativeURL)Other URL Constructors
The URL class provides two additional constructors for creating a URL object. You would use these constructors when you 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 know the various components of the URL but did not have a String containing the complete URL,For example, if you had designed 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 would 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:This is equivalent toURL gamelan = new URL("http", "www.gamelan.com", "/Gamelan.network.html");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 absolute 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.
This creates a URL object for this URL:URL gamelan = new URL("http", "www.gamelan.com", 80, "/Gamelan.network.html");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 equivalenttoExternalForm()
method.MalformedURLException
Each of the four URL constructors will throw a MalformedURLException if the arguments to the constructor refer to a null or unknown protocol. Typically, your Java programs will want to catch and handle this exception. Thus you would normally embed your URL constructor statements in a try/catch pair.See Handling Errors Using Exceptions for information about handling exceptions.try { URL myURL = new URL(. . .) } catch (MalformedURLException e) { . . . // exception handler code here . . . }
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 |