Creating a URL

The easiest way to create a URL object is from a string that represents the human-readable form of the URL. This is typically the form that someone else will use for a URL. In your Java program, you can use a string containing this text to create a URL object:

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

Creating one URL relative to another

A relative URL contains just enough information to get to the relative (or at the same time) resource. context of) another URL.

Relative URL specifications are often used in HTML files. For example, suppose you write an HTML file named JoesHomePage.html. Within this page are links to other pages, PicturesOfMe.html and MyKids.html, which 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 as filenames, like this:

These URLs are relative URLs. That is, URLs are specified relative to the file they are contained in: JoesHomePage.html.

In your Java programs, you can create a URL object from a relative URL specification. For example, suppose you know of two URLs on the site example.com:

This code snippet uses the URL constructor which allows you to create a URL object from another URL object (the base). and a relative URL specification. The general form of this constructor is:

See Also:  17 Things You Need for Effective Blog Management

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

This constructor is also useful for creating URL objects for named anchors (also called references) within a file. For example, suppose the file page1.html has a named anchor named BOTTOM at the bottom of the file. You can use the relative URL constructor to create a URL object like this:

Other URL Constructors

The URL class provides two additional constructors for creating a URL object. These constructors are useful when working with URLs, such as HTTP URLs, that have hostname, filename, port number, and reference components in the resource name portion of the URL. These two constructors are useful when you don’t have a string that contains the full URL specification, but you know several components of the URL.

For example, suppose you design a network navigation panel similar to a dashboard. file browsing. panel that allows users to choose the protocol, host name, port number, and file name. You can build a URL from panel components. The first constructor creates a URL object from a protocol, a hostname, and a filename. The following code snippet creates a URL to the page1.html file on the example.com site:

See Also:  How much should your logo design cost?

This is equivalent to

The first argument is the protocol, the second is the host name, and the last one is the path name of the file. Note that the file name contains a leading slash. This indicates that the file name 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 the following URL:

If you construct a URL object using one of these constructors, you can get a string containing the full URL using the URL object’s toString method or the equivalent toExternalForm method .

URLs with special characters

Some URLs contain special characters, for example, the space character. Like this:

For these characters to be legal, they must be encoded before being passed to the URL constructor.

Encoding the special characters in this example is easy as there is only one character you need encoding, but for URLs that have multiple of these characters, or if you’re not sure when writing your code which URLs you’ll need to access, you can use the multi-argument constructors of the java.net.URI class to automatically take care of the encoding. encoding for you.

And then convert the URI to a URL.

MalformedURLException

Each of the four URL constructors throws a MalformedURLException if the arguments of the constructor refer to a null or unknown protocol. Typically, you’ll want to catch and handle this exception by embedding your URL constructor declarations in a try/catch pair, like this:

See Also:  3 steps to create beautiful and engaging emails with Google Docs

See Exceptions for information on exception handling.

.

Leave a Reply

Your email address will not be published. Required fields are marked *