<< Chapter < Page | Chapter >> Page > |
As you can see from Listing 1 , there is a method available for extracting each of the parts of a URL that were identified in the above list .
Beginning of the program named Java4640a
Now that we know what the display method does, we can examine the code in the main method of the class.
Listing 2 shows the beginning of the program named Java4640a and the beginning of the main method.
Listing 2 - Beginning of the program named Java4640a. |
---|
import java.net.*;
import java.io.*;class Java4640a{
public static void main(String[]args){
Java4640a obj = new Java4640a();try{
System.out.println("Use simple string constructor for host URL");
obj.display(new URL("http://www.austincc.edu")); |
Listing 2 illustrates the instantiation of a URL object using the version of the constructor that expects to receive the URL in string format. (I will ignore the exception handling code from these discussions for brevity.)
Listing 2 begins by instantiating an object of the controlling class that can be used to access the display method. Then it instantiates a new URL object using the string-parameterversion of the constructor and passes that object to the display method.
As described above, the display method accesses each component part of the URL object and displays them separatedby spaces. Then it displays the URL object using the overridden toString method.
Program output for string-parameter constructor
The code in Listing 2 produced the output shown in Figure 3 .
Figure 3 - Program output for string-parameter constructor. |
---|
Use simple string constructor for host URL
http www.austincc.edu -1 nullhttp://www.austincc.edu |
The -1 in Figure 3 indicates that there was no port specification, and the null indicates that there was no file name specification in the URL passed to the constructor for the URL object.
The code fragment in Listing 2 is followed by code that constructs the URL object using other overloaded versions of the constructor.Each overloaded version requires the URL information in different formats. You can view that code in Listing 8 .
Building an absolute URL
I will to skip that code and move down to a more interesting case as shown by the fragment in Listing 3 .
Listing 3 - Building an absolute URL. |
---|
System.out.println("Construct absolute URL from " +
"host URL and relative URL");URL baseURL = new URL(
"http://www.austincc.edu/baldwin/hello.html");obj.display(new URL(baseURL,"/baldwin/Index.html")); |
Listing 3 uses a URL constructor that requires two parameters: a URL object and a String object. Here is part of the somewhat cryptic description of this constructor from the Oracle documentation.
"Creates a URL by parsing the given spec within a specified context. The new URL is created from the given context URL and the spec argument asdescribed in RFC2396 "Uniform Resource Identifiers : Generic * Syntax" : "
What does this mean?
Let me try to explain this constructor in my own words (with some help from Elliotte Rusty Harold) . You can use this constructor to build an absolute URL from a relative URL .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?