This week we're going back to the concrete again, with the start of a few articles on Java networking. Back to Basics
The ability to use the network is probably one of the more intriguing aspects of the Java system--one more feature that separated it completely from most of the other general-purpose innovations devised for the Web. Many programmers and designers of online content are ecstatic over the possibilities the networking features of Java represent, and rightly so. With a robust network facility in its repertoire, the Java applet suddenly holds possibilities of vast, effortless, and platform-universal, client-server, or peer-to-peer applications, all wrapped within the comforting accessibility of the Web page.
Already there are dozens of major commercial undertakings that rely on the Java applet as an intelligent agent that can be deployed to a client's computer, always in contact with its server, relaying useful information back and forth. Much to the delight of site designers, chat rooms and other diversions, which until a year ago usually required separate applications, can now appear magically on the screens of the millions of Web users too uninterested to seek them out otherwise.
As programmers, it's exciting to see how easy it is to use the network in our Java programs. As with many of the other features of Java we covered thus far, the network portion of the API reflects the refinement of a next-generation language. It's wrapped in the consistent and sensible (for the most part) form and style of the language, with defaults and powerful shortcuts already built in. For simplicity's sake, we're going to start with one.
It's logical that the Web is the main focal point for Java programmers. Aside from being the chosen medium of introduction (although it was not originally intended to be that way from the beginning--Sun merely saw how it could be adapted to the task and did so), the Web metaphor, as it were, embodies a number of features (e.g., for encompassing various protocols) that make it an ideal, programmer-friendly interface with which to equip the language.
Within the Java API, you will find an entire subsection (perhaps arguably an entire two subsections) devoted to network communications. Within
java.netyou'll find fewer than a dozen classes that embody the entire Java networking package, which is indicative of the powerful simplicity (and well-laid-out object duties) with which this package was executed.Some of you may not be familiar with a few of the more obscure networking terms (datagrams and sockets, to be defined later here and described briefly in the Java networking tutorial page), but most likely anyone reading this on the World-Wide Web will notice that there is a URL class (
java.net.URL).Those of you who've been following along with us for a while will not be surprised that URLs, network packets, network connections, and the rest are all represented by Java classes, to be objects in your own programs. To summarize briefly, the HTTP protocol (which the Web is based on, largely) has been implemented in Java's networking scheme and can be manipulated with surprising ease by the object. For your applet to load the contents of a Web page, for instance, or to receive the output of a CGI on a Web server, you need only create a URL object, containing the URL of your target page, for instance, and then a use that to open a URLConnection object. URLConnections are versatile; you can use them to read the Web page like you would read a file, using a Stream (this is from java.io, the more generalized IO section of the API that we'll cover at a later date), or you can use the more sophisticated content-handler system devised to create and load appropriate object types from whatever you receive. Writing to the URL can also, theoretically, be accomplished in the same manner.
There's a lot here to look at, and we encourage you to check out the Java tutorial on this subject, as mentioned earlier. In addition to URLs and HTTP, there are facilities for more general, and powerful, network connections of two other types, all of which we'll get to in the coming weeks.
As you can imagine, almost exactly by virtue of being a powerful programming tool, the ability of Java to communicate in this fashion is also a source for major security headaches and is in fact rumored to be punching holes through firewalls everywhere as we speak. All this and more next time.