Creating a URL (2024)

A relative URL contains only enough information to reach the resourcerelative 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.htmland 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:

<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 arespecified relative to the file in which they are contained--JoesHomePage.html.

In your Java programs,you can create a URLobject from a relative URL specification.For example, suppose you know two URLs at the Gamelan site:

http://www.gamelan.com/pages/Gamelan.game.htmlhttp://www.gamelan.com/pages/Gamelan.net.html
You can create URLobjects for these pages relativeto their common base URL:http://www.gamelan.com/pages/ like this:
URL gamelan = new URL("http://www.gamelan.com/pages/");URL gamelanGames = new URL(gamelan, "Gamelan.game.html");URL gamelanNetwork = new URL(gamelan, "Gamelan.net.html");
This code snippet uses the URL constructorthat lets you create a URLobject from another URL object (the base) and a relative URLspecification. The general form of this constructor is:
URL(URL baseURL, String relativeURL)
The first argument is a 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 URLobjects for named anchors (also called references) within a file.For example, suppose the Gamelan.network.htmlfile 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");
The URL class provides two additional constructors for creating a URLobject. 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 URLfrom 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:

new URL("http", "www.gamelan.com", "/pages/Gamelan.net.html");
This is equivalent to
new 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.

The final URL constructor adds the port number to the listof arguments used in the previous constructor:

URL gamelan = new URL("http", "www.gamelan.com", 80, "pages/Gamelan.network.html");
This creates a URL object for the following URL:
http://www.gamelan.com:80/pages/Gamelan.network.html
If you construct a URL object using one of theseconstructors, you can get a Stringcontaining the complete URL addressby using the URL object's toString method or theequivalent toExternalForm method.
Creating a URL (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6506

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.