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 andin the same directory as JoesHomePage.html
. The links toPicturesOfMe.html
and MyKids.html
fromJoesHomePage.html
could be specified just as filenames,like this:
These URL addresses are relative URLs. That is, the URLs arespecified 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 you know two URLs at the Gamelan site:
You can createhttp://www.gamelan.com/pages/Gamelan.game.htmlhttp://www.gamelan.com/pages/Gamelan.net.html
URL
objects for these pages relativeto their common base URL:http://www.gamelan.com/pages/
like this:This code snippet uses theURL gamelan = new URL("http://www.gamelan.com/pages/");URL gamelanGames = new URL(gamelan, "Gamelan.game.html");URL gamelanNetwork = new URL(gamelan, "Gamelan.net.html");
URL
constructorthat lets you create a URL
object from another URL
object (the base) and a relative URLspecification. The general form of this constructor is:The first argument is aURL(URL baseURL, String relativeURL)
URL
objectthat specifies the base of the newURL
.The second argument is a String
that specifies the rest of theresource name relative to the base. If baseURL
is null, then thisconstructor treats relativeURL
like an absolute URL specification.Conversely, if relativeURL
is an absolute URL specification,then the constructor ignores baseURL
.This constructor is also useful for creating URL
objects for named anchors (also called references) within a file.For example, suppose the Gamelan.network.html
file has a named anchor called BOTTOM
at thebottom of the file. You can use the relative URL constructor to createa URL
object for it like this:
URL gamelanNetworkBottom = new URL(gamelanNetwork, "#BOTTOM");
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, andreference components in the resource name portion of the URL. These twoconstructors are useful when you do not have a String containing thecomplete URL specification, but you do know various components of theURL.For example, suppose you design a network browsing panel similar to afile browsing panel that allows users to choose the protocol, hostname, port number, and filename. You can construct a URL
from the panel's components. The first constructor creates aURL
object from a protocol, host name, and filename. Thefollowing code snippet creates a URL
to theGamelan.net.html
file at the Gamelan site:
This is equivalent tonew URL("http", "www.gamelan.com", "/pages/Gamelan.net.html");
The first argument is the protocol, the second is the host name, andthe last is the pathname of the file. Note that the filename contains aforward slash at the beginning. This indicates that the filename isspecified from the root of the host.new URL("http://www.gamelan.com/pages/Gamelan.net.html");
The final URL
constructor adds the port number to the listof arguments used in the previous constructor:
This creates aURL gamelan = new URL("http", "www.gamelan.com", 80, "pages/Gamelan.network.html");
URL
object for the following URL: If you construct ahttp://www.gamelan.com:80/pages/Gamelan.network.html
URL
object using one of theseconstructors, you can get a String
containing the complete URL addressby using the URL
object's toString
method or theequivalent toExternalForm
method.