Java’s networking classes enable you to implement applications that communicate across a network/remote connection, but the platform also extends into the realm of the Internet and URLs. Java's URL class makes accessing Web resources as easy as accessing a local file. Let's take a look at how you can tap into the power of the URL class and read and write data over URL connections.
Check out these related articles
"Handling Java I/O via remote connections"
"Read and write data with Java’s I/O streams"
"Transitioning into OOP: Abstract data types in Java"
Working with URLs
A URL identifies resources such as files, Web pages, and Web applications that exist on the Web. It consists of a number of syntactic elements. For example, note the following URL:
The protocol element is identified as http. The host name is www.mycompany.com. The port number is 8080. The rest of the URL, /mywebapps/AWebApp, identifies the resource to be accessed on the site. The resource, in this case, happens to be a Web application. URLs can also include other elements, such as fragments and query strings.
Data that is retrieved from a URL can be diverse, which necessitates a uniform mechanism for reading from and writing to URLs. Java offers such a mechanism in its java.net package. The specific class from this package that we want to discuss is the URL class.
The URL class is an abstraction of the URL identifier. It allows a Java programmer to open a connection to a specific URL, read data from it, write data to it, read and write header information, and perform other operations on the URL. We will discuss how the URL class and the stream classes provided by the java.io package allow you to operate on a URL in much the same manner that you operate on files and socket connections.
When creating an instance of the java.net.URL class, you can take advantage of a number of public constructors to gain flexibility. For example, the class offers a constructor that takes a complete URL string, a constructor that takes a URL string that is broken up into protocol, host, and file/resource, and a constructor that takes a URL string that is broken up into protocol, host, port, and file. Let’s construct an instance of the URL class using a complete URL:
URL aURL = new URL(“http://www.mycompany.com:8080/index.html”);
In this example, an instance of the URL class is created with a complete URL that designates the protocol as http, the host as www.mycompany.com, the port as 8080, and the file/resource as index.html. Each of the constructors of the URL class throw a MalformedURLException in the event that the arguments passed in form a URL that is syntactically incorrect.
Opening a URL connection
Once you have successfully created an instance of the URL class, you can begin to call operations on it. But before you can access the resource or content represented by the URL, you must open a connection to it. You can do this with the openConnection method call.
The openConnection method takes no parameters and, on success, returns an instance of the URLConnection class. Listing A demonstrates the process of opening a connection to a URL. Once you have a successful connection, you can begin reading and writing to the input and output streams of the URLConnection instance.
Reading from a URL
Using the java.io stream classes to read from a URL is a simple process. Once you have a successful connection, you can retrieve the input stream for the connection and begin reading. URLs can represent resources consisting of a wide variety of data formats. Fortunately, the java.io classes can operate on data returned from URLConnection streams in the same fashion that they operate on file streams or socket streams. Listing B shows how to read text data from a URL.
Writing to a URL
It's also simple to write to a URL using the java.io stream classes. Once you have a successful connection, you can retrieve the output stream for the connection and begin writing. Of course, it only makes sense to write to a connection that is expecting data from a client. Also, before retrieving and writing to a URLConnection stream, you need to designate the connection as being write-enabled by setting the Output property to true using the setDoOutput(boolean) method. The java.io classes allow you to write data to URLConnection streams just as you write to file streams or socket streams. Listing C demonstrates how to write object data to a URL.
You can retrieve other types of information from URL and URLConnection objects, such as the host, port, content length, content encoding, and content type. Using these methods along with the stream I/O classes enables you to build sophisticated Web client applications and services.
Easy access to the Web
The URL class provided by the Java platform enables you to access Web resources with the same power and ease you enjoy when accessing a local file. You don't have to worry about the details of Web communication and can concentrate instead on building useful applications and services around Web resources.
In the next article, we'll cross over to the server-side of Java, which will open up a whole new world of functionality for building powerful enterprise applications and services.