<< Chapter < Page Chapter >> Page >

Assume, for example, that you have written your own method to display HTML files the way that they are displayed by a browser rather than simply as a text file. Such files often contain links to relative URL's . In such a case, the link would be provided simply as a path and file name under theassumption that the path and file name can be found relative to the base URL containing the HTML file.

According to Java Network Programming by Elliotte Rusty Harold,

"In this case, you use the URL to the document that contains the link to provide the missing information."

The construction process

The code in Listing 3 constructs a base URL object pointing to

"http://www.austincc.edu/baldwin/hello.html"

Then it uses the version of the constructor currently under discussion to combine that base URL object with a relative URL given by

"/baldwin/Index.html"

This produces the URL object displayed in Figure 4 .

Figure 4 - Building an absolute URL.
Construct absolute URL from host URL and relative URL http www.austincc.edu -1 /baldwin/Index.html nullhttp://www.austincc.edu/baldwin/Index.html

Hopefully this example illustrates how the constructor can combine a base URL object with a relative URL to producea new URL object that is an absolute pointer to the relative URL.

The URLEncoder class

There is one more issue that we need to examine before leaving this program: the URLEncoder class. This class is provided to help deal with problems arising from spaces, special characters, non-alphanumeric characters, etc. , that some operating systems may allow in file names but which may not be allowed in aURL.

If you need to create a URL object using a URL string that has these problems, you should first use the encode method of the URLEncoder class to convert it into an acceptable URL string.

The URLEncoder.encode method

This class provides a static method named encode that encodes a string representation of a URL into an acceptable format.

(Technically I believe it is correct to say that the format produced in Listing 4 is"application/x-www-form-urlencoded" and the binary encoding is UTF-8.)

The encode method returns a String object that is a cleaned-up version of the original string.

Listing 4 calls the encode method to encode a string that was purposely constructed to contain several unsafe characters. It displays boththe raw string and the encoded string for comparison.

Listing 4 - The URLEncoder.encode method.
System.out.println("Now use URLEncoder to create " + "UTF-8 encoded String");System.out.println("http://space .tilde~.plus+.com"); System.out.println(URLEncoder.encode("http://space .tilde~.plus+.com","UTF-8"));

The encoded output

The output from the code in Listing 4 is shown in Figure 5 .

Figure 5 - An encoded string.
Now use URLEncoder to create UTF-8 encoded String http://space .tilde~.plus+.comhttp%3A%2F%2Fspace+.tilde%7E.plus%2B.com

The encoded version doesn't mean a lot to a human, but it is a format that is acceptable across a wide variety of computers. In case you areinterested, the encoding rules are shown in Figure 6 .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask