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


What is a URL?

If you've been surfing the World Wide Web, you have undoubtedly heard the term URL and used URLs to access various HTML pages from the Web. So, what exactly is a URL? Well, the following is a fairly simple, but formal definition of URL:


Definition: URL is an acronym that stands for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.

It's often easiest (though not entirely accurate) to think of a URL as the name of a file on the network because most URLs refer to a file on some machine on the network. However, you should remember that URLs can point to other resources on the network such as database queries and command output.

The following is an example of a URL:

http://java.sun.com/
This particular URL addresses the Java Web site hosted by Sun Microsystems. The URL shown above, like all other URLs, has two main components: In the example, http is the protocol identifier and //java.sun.com/ is the resource name.

The protocol identifier indicates the name of the protocol to be used to fetch the resource. The example uses the HTTP protocol which is typically used to serve hypertext documents. The HTTP protocol is just one of many different protocols used to access different types of resources on the net. Other protocols include ftp, gopher, file, and news.

The resource name is the complete address to the resource. The format of the resource name depends entirely on the protocol used, but for many formats the resource name will contain one or more of the following components:

host name
the name of the machine the resource lives on
filename
the pathname to the file on the machine
port number
the port number to connect to (this is typically optional)
reference
a reference to a named anchor within a resource, usually identifies a specific location within a file
For many protocols, the host name and the filename are required and the port number and reference are optional. For example, the resource name for an HTTP URL must specify a server on the network (host name) and the path to the document on that machine (filename), and may also specify a port number.

When constructing any URL, put the protocol identifer first, followed by a colon (:), followed by the resource name, like this:

protocolID:resourceName

The java.net package contains a class named URL which your Java programs use to represent a URL address. Your Java program can construct a URL object, open a connection to it, and read to and write from it. The remaining pages of this lesson show you how to work with URL objects in your Java programs.

See also

java.net.URL


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