Skip to main content

About this book

Since the second edition of this text, the use of the Internet and networks generally has continued to expand at a phenomenal rate. This has led to both an increase in demand for network software and to improvements in the technology used to run such networks, with the latter naturally leading to changes in the former. During this time, the Java libraries have been updated to keep up with the new developments in network technology, so that the Java programming language continues to be one of the mainstays of network software development.

In providing a very readable text that avoids getting immersed in low-level technical details, while still providing a useful, practical guide to network programming for both undergraduates and busy IT professionals, this third edition continues the trend of its predecessors. To retain its currency, the text has been updated to reflect changes that have taken place in Java's network technology over the past seven years (including the release of Java 7), whilst retaining its notable features of numerous code examples, screenshots and end-of-chapter exercises.

Table of Contents

Chapter 1. Basic Concepts, Protocols and Terminology

The fundamental purpose of this opening chapter is to introduce the underpinning network principles and associated terminology with which the reader will need to be familiar in order to make sense of the later chapters of this book. The material covered here is entirely generic (as far as any programming language is concerned) and it is not until the next chapter that we shall begin to consider how Java may be used in network programming. If the meaning of any term covered here is not clear when that term is later encountered in context, the reader should refer back to this chapter to refresh his/her memory.
Jan Graba

Chapter 2. Starting Network Programming in Java

Having covered fundamental network protocols and techniques in a generic fashion in Chap. 1, it is now time to consider how those protocols may be used and the techniques implemented in Java. Core package contains a number of very useful classes that allow programmers to carry out network programming very easily. Package java x .net, introduced in J2SE 1.4, contains factory classes for creating sockets in an implementation-independent fashion. Using classes from these packages (primarily from the former), the network programmer can communicate with any server on the Internet or implement his/her own Internet server.
Jan Graba

Chapter 3. Multithreading and Multiplexing

It is often the case nowadays that programs need to carry out more than one significant task at the same time (i.e., ‘concurrently’). For example, a GUI-driven program may be displaying a background animation while processing the user’s foreground interactions with the interface, or a Web browser may need to download and display the contents of a graphics file while rendering the rest of the associated Web page. The popularity of client/server applications over the past decade has exacerbated this demand enormously, with server programs sometimes having to process the needs of several hundreds of clients at the same time.
Jan Graba

Chapter 4. File Handling

With all our programs so far, there has been a very fundamental limitation: all data accepted is held only for as long as the program remains active. As soon as the program finishes execution, any data that has been entered and the results of processing such data are thrown away. Of course, for very many real-life applications (banking, stock control, financial accounting, etc.), this limitation is simply not realistic. These applications demand persistent data storage. That is to say, data must be maintained in a permanent state, such that it is available for subsequent further processing. The most common way of providing such persistent storage is to use disc files. Java provides such a facility, with the access to such files being either serial or random. The following sections explain the use of these two file access methods, firstly for non-GUI applications and later for GUI applications. In addition, the important and often neglected topic of serialisation is covered.
Jan Graba

Chapter 5. Remote Method Invocation (RMI)

With all our method calls so far, the objects upon which such methods have been invoked have been local. However, in a distributed environment, it is often desirable to be able to invoke methods on remote objects (i.e., on objects located on other systems). RMI (Remote Method Invocation) provides a platform-independent means of doing just this. Under RMI, the networking details required by explicit programming of streams and sockets disappear and the fact that an object is located remotely is almost transparent to the Java programmer. Once a reference to the remote object has been obtained, the methods of that object may be invoked in exactly the same way as those of local objects. Behind the scenes, of course, RMI will be making use of byte streams to transfer data and method invocations, but all of this is handled automatically by the RMI infrastructure. RMI has been a core component of Java from the earliest release of the language, but has undergone some evolutionary changes since its original specification.
Jan Graba

Chapter 6. CORBA

Though RMI is a powerful mechanism for distributing and processing objects in a platform-independent manner, it has one significant drawback—it works only with objects that have been created using Java. Convenient though it might be if Java were the only language used for creating software objects, this simply is not the case in the real world. A more generic approach to the development of distributed systems is offered by CORBA (Common Object Request Broker Architecture), which allows objects written in a variety of programming languages to be accessed by client programs which themselves may be written in a variety of programming languages.
Jan Graba

Chapter 7. Java Database Connectivity (JDBC)

The previous three chapters employed individual, ‘flat’ files to provide persistent data storage. Nowadays, of course, most organisations have the bulk of their data structured into databases, which often need to be accessed from more than one site. These databases are almost invariably relational databases. Programs written in Java are able to communicate with relational databases (whether local or remote) via the Java Database Connectivity (JDBC) API, which became part of the core Java distribution with JDK 1.1. In this chapter, we shall consider how such databases may be accessed via JDBC.
Jan Graba

Chapter 8. Servlets

HTML (HyperText Markup Language) is the tagging language used to create Web pages. In order to appreciate fully the material presented in this chapter, it will be necessary to have at least a rudimentary knowledge of HTML. If you do not have such knowledge, you are advised to consult the early chapters of one of the widely-available HTML texts before reading any further.
Jan Graba

Chapter 9. JavaServer Pages (JSPs)

Since JSPs serve essentially the same purpose as servlets, it is natural to ask why there is a need for both. The simple answer is that servlets require the expertise of Java programmers, whilst the production of Web pages for anything more than a simple site is usually the responsibility of Web page authors, who often do not have such programming skills. The introduction of the JavaServer Pages technology attempts to return the job of Web page authoring to those people whose responsibility it is, whilst the Java programmers maintain responsibility for the software components used upon the Web pages. Using JSPs rather than servlets also removes the rather tedious repetition of out.println for HTML tags.
Jan Graba

Chapter 10. JavaBeans

For a number of years, one of the primary goals of software engineering has been to create and make use of general-purpose software components that may be ‘plugged’ into a variety of applications. The internal workings of such components are hidden from the application developer and are of no concern to him/her. The only things that the developer needs to know are what purpose the component serves and what interface it provides (i.e., what parameters need to be passed to the component and what value(s) the component will return). The major advantages of such components are fairly obvious:
Jan Graba

Chapter 11. Multimedia

In Java, classes Image (from package java.awt) and ImageIcon (package javax.swing) are used for holding and manipulating images. Either may be used on its own, but ImageIcon is particularly useful for loading an image from the current directory into an application. For example:
Jan Graba

Chapter 12. Applets

Though an applet must have a GUI, its containing class does not extend JFrame. This is hardly surprising, of course, since applets pre-date the Swing classes, but the applet’s containing class does not extend class Frame either. Before the Swing classes appeared, an applet consisted of a class that extended class Applet (from package java.applet). The introduction of the Swing classes brought in class JApplet (package javax.swing), which extends class Applet and makes use of the other Swing classes. Thus, later applets should extend class JApplet. Unfortunately, there are major differences of operation between applets that use only pre-Swing classes and those that use the Swing classes. ‘Differences in operation’ is actually putting it very mildly. A lot of Swing applets will simply not work in some of the earlier versions of Internet Explorer and Netscape! However, this problem has been eradicated in the latest versions of the major browsers, as will be seen in the next section. Since the Swing classes have been around for so long now, most of what follows will refer to Swing applets only, and the term ‘applets’ will be used without qualification. In 12.4.1, two pre-Swing examples are used, but these are identified as such explicitly.
Jan Graba
Additional information