Web pages can have scripts (VBScript, JScript, or Java Script) or Java applets embedded in them, which are then executed on the client’s machine once the page has downloaded. These scripts and applets can add interactivity to the Web pages and can perform specialized functions such as data validation on data input into forms. In addition, they take some of the processing burden off the Web server.
A lot of media attention has surrounded the Java programming language.
There are two main reasons for this. First, Sun (the company that developed Java) has to compete in the same environment as Microsoft. To get noticed in this environment, there has to be concerted promotion of the product. The sec- ond reason is that Java is a sound language that has a number of innovative features [1].
Table 7.1 FrontPage Features
Explorer Features Editor Features
Can modify templates and Wizards to quickly develop a Web site
Can view a Web site in up to seven views Can check spelling throughout an entire Web site Allows definitions of permissions for access
by development team members Can be published to the Web facility Provides editor features
Can add and develop new pages Provides six toolbars
Can format text Creates interactive forms Can add hotspots to images Can draw and edit tables
Can insert FrontPage components and special utilities
Figure 7.1 FrontPage Explorer.
Figure 7.2 FrontPage Editor.
Sun developed a language called Oak that was used for consumer elec- tronic devices. This language was small, reliable, and architecture independent.
The developers realized that with the rise of the Internet, there was a need for an architecture neutral language, hence, the development of Java from Oak.
Sun’s white paper [2] on the Java language describes it as follows:
Java: A simple, object-oriented, distributed, interpreted, robust, secure, archi- tecture neutral, portable, high-performance, multithreaded and dynamic language.
Although this description might seem too good to be true, the following sections concentrate on five important areas of software technology that are present within the Java language.
Open Systems
Java is an interpreted language. Interpreters exist as part of the Java Develop- ment Toolkit (JDK) or are included in Internet browsers, such as Netscape (version 2.0 or later). Although the language is interpreted, Java source code is also compiled. The compilation process creates what is termed a byte code file (Figure 7.3). These byte codes can be interpreted by any Java interpreter, irrespective of the platform (of course, as long as there is an interpreter on that platform). Java interpreters are available for UNIX, Windows 95, Win- dows NT, and Macintosh operating systems.
Javac = java compiler
HTML document calls class file
<APPLET
Code="ClassName.class">
Java Code ClassName.java
Bytecode ClassName.class Browser interprets class file
Figure 7.3 Java compilation and interpretation process.
The byte code files provide the portability from one platform to another since the platform-dependent features of the language can be drawn in at the interpreted point of running the program. Collectively, the interpreter and run-time system implement a virtual machine called the Java Virtual Machine [3]. Hence, Java applets (small graphical programs) can be downloaded over the Internet from one type of platform and be run on another (Figure 7.4).
This single feature has been a revelation for the Internet and holds great prom- ise for the future of programming since it has been referred to as a “write once, run anywhere” development approach [4]. At this level, Java has created a breakthrough in the evolution of open systems software.
A Java-enabled browser, when accessing a Web site, downloads the HTML file to the local host. The browser recognizes a special hypertext tag called Applet. This means that the browser knows that an applet is associated with that Web page. The browser downloads another file of information—the byte code file. The Java-enabled browser interprets these byte codes and runs them as an executable program on the user’s host. The downloading is the start of applet execution and happens automatically. Through this method Java delivers executable content over networks [5].
Java started out as a consumer electronic device language, but it has gained greater notoriety as an Internet language. It appears to have set a stan- dard for computer programming languages with respect to portability features.
Importantly, it is setting a standard for open systems on a software level.
Java browser User host
Byte code
H/w & S/w interface
Provider server
HTML document APPLET tag
Byte code
Java code
Compiler
Figure 7.4 Java’s operation on the Web.
GUI and Multimedia Development
Graphical components provide a mechanism in GUIs for inputting, selecting, and outputting data. Java provides a full range of graphical components. These include the following (Figure 7.5):
• Text fields/text area: components for inputting and outputting text;
• Checkbox: a small square that is clicked on to choose an option;
• Panel: a container (invisible) of group components;
• Label: a text item;
• Button: an object used to select or create an event;
• List: a scrollable list of text items from which the user can choose.
Some versions of Java include a graphical user interface development environment, for example, VisualAge Java produced by IBM (see Chapter 6).
Button
List
Scrollbar
Checkbox
Label
Text field
Text area
Figure 7.5 A selection of Java components.
With these versions the graphical components can be dragged, dropped, moved, and resized under mouse control.
Java also includes a full range of events to manipulate the GUI. These include the standard mouse and keyboard-driven events. Components and events have raised the interactivity of Web pages. Pre-Java Web pages provided selectivity through hypertext links, but their level of interactivity was low. Java has raised the interactive quality of the Web because an applet can accept and process input from the user and respond in a customized fashion [5].
In addition to components, Java allows the development of animation in text or graphical format. Of course, sound and video clips can be incorporated into Java applets providing a rich multimedia environment in which students can work.
Object-Oriented Language
Java is an object-oriented language and is a good language to demonstrate object-oriented principles. This is because it was developed using a clean slate approach [5]. In other words, Java was not designed to be source compatible with any other language. The object model in Java is simple and easy to extend, while simple types, such as integers, are kept as high-performance non-objects.
Java supports the following object-oriented constructs.
Classes and Objects
Classes act as templates for the creation of objects and hence the class construct is reused. The code listing that follows creates a class called Container with three data items and a method to compute the volume of the container.
Parameters are passed from the ContainerDem class when objects are created and the data for the object are initialized by the Container constructor. Two objects are created: container1 and container2 in ContainerDem. The volume method is then called for each object, and the results returned and displayed in the DOS window.
// Java program to demonstrate the use of classes and objects // Container uses a constructor to
// initialize the dimensions of a container.
class Container { double width;
double length;
double depth;
// The constructor for container.
Container(double w, double l, double d) { width = w;
height = l;
depth = d;
}
// The volume of the container is returned double volume() {
return width * length * depth;
} }
class ContainerDem {
public static void main(String args[]) { // declare and initialize container objects
Container container1 = new Container(5, 10, 15);
Container container2 = new Container(4, 6, 10);
double vol;
// Use the volume method for the first container vol = container1.volume();
System.out.println(“The volume of container1 is “ + vol);
// Use the volume method for the second container vol = container2.volume();
System.out.println(“The volume of container2 is “ + vol);
} } Inheritance
Inheritance is an important concept in the object-oriented paradigm. Classes can be arranged hierarchically with the most general features in the top-level class or superclass and the more specific features in the subclasses. The subclasses inherit all instance variables and methods of the superclasses. The important thing about all of this is that, first, the software becomes more modular and, second, the superclasses are being reused instead of redefining them all in the subclasses.
Abstract Methods and Interfaces
An interface is a prototype for a class. It can be viewed as a logical design. Inter- faces are abstract classes that are completely unimplemented, that is, no methods in the class have been implemented. Abstract classes usually have some
methods implemented. Interfaces have similar benefits to abstract classes. They provide a means to define the protocols for a class without worrying about the implementation details. This enables large projects to be more easily managed.
The types of methods (and names) are clarified up front.
Although Java does not allow multiple inheritance, it does allow multiple interfaces. The interface approach only allows you to inherit method descrip- tions, not implementation details. This feature is particularly useful when working in project teams.
Polymorphism With Constructors
A constructor is a method that has the same name as the class it is in. Once defined the constructor is called immediately after the object on that class is created. Constructors have no return type because they are the same type as the class. The constructor initializes the internal state of the object.
One of the ways that Java implements polymorphism is through the over- loading of methods. Polymorphism is a term used in programming in which one feature or method is used many times. Overloading of a method is done by having more than one method of the same name, but where the methods differ is in the number and/or types of the arguments. Its usefulness is in situations where the processing requires some flexibility, such as a method being able to deal with data of string or integer type. It means the same method name can basically be used to do the processing and so the concept aids the reuse of software.
The benefits of object-oriented programming are well documented. Gra- ham [6] summarizes the benefits as reusability of software components. This is achieved through inheritance and the class and object constructs.
Threads and Multithreading
Multithreading is an important concept within Java. Multithreading allows lines of execution to be performed concurrently within the application program.
The programmer can specify threads of execution, each thread designating a portion of a program that may execute concurrently with other threads. Two examples will suffice to show the usefulness of threads. First, when a large file such as an audio or video clip is downloaded over the Web, we may want the clip to start playing before it has entirely downloaded. The two tasks can be allocated to threads and coordinated so that there is always enough video to play, thus avoiding choppy playback. A second example is in the automatic gar- bage collection utility within Java. The garbage collector automatically reclaims dynamically allocated memory that is no longer needed. This garbage collector runs as a low-priority thread that runs when processor time is available [7].
Distributed Computing on the Internet
The way in which Java works in a distributed environment is related to the platform-neutral design of the language, but it goes beyond this. Java is unique in its distributed nature: “It feels like there aren’t boundaries between machines” [2]. This is achieved through a combination of techniques. Classes form an encapsulation boundary for components that are transferred over the Web. Also behavior is kept separate from data; the code has tags to link to the data, such as image files.
Security is an important issue in distributed computing and particularly so with the Internet. Using a Java-compatible Web browser, it is possible to safely download Java applets without the fear of potential virus infection or malicious interest [7]. A Java program is confined to the “Java execution envi- ronment” and does not allow it access to other parts of the computer. The facility to download applets without fear of security breaches is seen by some as the most important aspect of Java [7].
Java can be connected to other applications, thereby tying together dis- tributed applications. In this respect, it is not a language in isolation but one that can glue together discrete systems. JavaBeans are software components that interconnect systems through the translation of various protocols. These com- ponents can be used to connect Java Web sites to the more traditional, core database applications such as Oracle and Access [8].
Java is more than a language. It is an infrastructure technology known as middleware. Sun is placing great emphasis, as part of its corporate strategy, on Web servers connected to desktop clients via the Internet, an intranet, or vir- tual private network. Powerful UNIX servers take the heavy processing load, with user interface functions performed by scaled-down desktop machines called thin clients. The Java Virtual Machine and JavaBeans are the glue that bonds the separate systems together.