- Special Edition Using Java, 2nd Edition -

Chapter 1

What Java Can Do For You


by Anil Hemrajani

By now you have probably heard enough hype about Java to go out and buy this book. The good news is that the hype is well-justified. Java is probably everything you have heard that it is and more.

In this chapter, you examine the various possibilities provided by the Java language by taking a look at the different types of applications you can develop with it. To drive the point home, you then take a look at several examples of Java applets that are currently on the Web. You also examine examples of a Java Graphical User Interface (GUI) application and a Java command line application. By the end of this chapter, you should have a fairly good idea of what you can accomplish with Java and be excited about this new language and how it is changing the computing world.

The Four Types of Java Applications

The inventors of Java admit that it is built on concepts that are borrowed from other languages, such as C, C++, Eiffel, SmallTalk, Objective C, and Cedar/Mes. So, it is not surprising that Java can accomplish similar tasks that these other languages tackle. For example, you can use C++ to build command line utilities, class libraries, GUI applications, and more. Java is no different in that respect. The following are the four types of applications you can build in Java:

Applets, the first type, are essentially mini applications that run inside a Java-enabled browser, such as Netscape 2.x/3.x, Microsoft Explorer 3.x, or HotJava.

The second type is your typical GUI application, such as the Windows Notepad application, which does not require a Web browser to execute it.

The third type is a command line application that can be run from an MS-DOS command prompt or a UNIX shell prompt, just like the xcopy command on MS-DOS or the ls command on UNIX.

The fourth type is not an application per se. Rather, it is more a collection of classes (portable Java-bytecode files) that belong to one package (similar to a C++ class library). There is no custom format for packages such as those used with static and dynamic libraries on the various operating systems. The implementation in Java is much more simple and portable.

Basically, all classes belonging to a package are placed in one directory. For example, all classes belonging to the Java’s Abstract Window Toolkit (AWT) package, java.awt, are placed in a directory called AWT under the C:\JAVA\CLASSES directory. This is a directory tree of various packages provided with the Java Development Kit:

c:\java\classes
|___applet
|___awt
| |___Button.class
| |___Color.class
| |___Event.class
|___io
|___lang
|___net
|___util

A few examples of class files under the awt directory are also shown to illustrate the point here (in actuality, there are approximately 49 class files under the AWT directory).

Learning About the Java Language

If you have ever read the White Papers on Java by Sun Microsystems, you realize that the description contains every possible buzz word available in the computing world for describing a programming language. Nonetheless, most of the buzz words fit the bill, so I use them here to describe Java.

Java is a simple, object-oriented, robust, secure, portable, high-performance, architecturally neutral, interpreted, multithreaded, dynamic language. Phew! Try saying all that in one breath. Anyway, the language itself is discussed in more detail in the remainder of this book, but the one buzz word I would like to touch upon here is interpreted.

Java source code compiles into portable bytecodes that require an interpreter to execute them. For applets, this task is handled by the browser. For GUI and command line applications, the Java interpreter program is required to execute the application. The examples shown in the section “Java Command Line Applications” later in this chapter illustrate both methods.

The Java Development Kit (JDK)

The reason Java became so popular was not only because of the benefits of the language itself, but the rich sets of packages (or class libraries to you C++ programmers) that come bundled with the JDK from Sun Microsystems also contribute to its popularity. These prewritten objects get you up and running with Java quickly, mainly because of two reasons:

Here is a brief description of some of the more significant packages provided with Java:

Package Description

Java Applets

As I mentioned previously, Java applets run within a Java-enabled Web browser. Because Web browsers were primarily developed for displaying HTML documents, incorporating Java applets inside a Web browser requires an HTML tag to invoke an applet. This HTML tag is the <APPLET> tag, as shown in the following example:

<applet code=TextEdit.class width=575 height=350></applet>

The previous example illustrates the basic required attributes. However, there are several other attributes and features in the <APPLET> tag that require an explanation. But before examining the APPLET tag, a brief explanation of how applets are downloaded from a server and executed by a Web browser (client) might be helpful.

The Applet Load Cycle

Because Java applets are referenced inside an HTML document and executed by a Web browser, it is not surprising that they reside on the server side, just as HTML documents do. There is a specific sequence of events that takes place when a Java-enabled browser loads an HTML document and detects the <APPLET> tag. Figure 1.1 illustrates some of the following steps that take place when loading an applet:

An HTML file is loaded.

The <APPLET> tag is detected.

The Applet class file is downloaded from the server.

Classes referenced by the applet class are detected and downloaded.

init() and start() methods are called in the applet class.

If all goes well, the applet is displayed inside your browser (or outside, if it uses its own Frame).


FIG. 1.1

The load cycle of an applet.

The bottom line is that an applet’s executable code (the class files) resides on a machine that is accessible to a Web server at a Web site. These class files are downloaded and executed when a user connects to the Web site and invokes the HTML document containing a reference (<APPLET> tag) to the applet.

The <APPLET> Tag

Listing 1.1 shows the syntax for the <APPLET> tag; the CODE, WIDTH, and HEIGHT attributes are required in this tag; the remaining attributes are optional.

Listing 1.1 Syntax of the <APPLET> tag.

<APPLET CODEBASE=url CODE=appletClassFile WIDTH=n HEIGHT=n
ALT=alternateText NAME=appletInstanceName ALIGN=alignment
VSPACE=n HSPACE=n>
<PARAM NAME=parameter1 VALUE=value1>
<PARAM NAME=parameter2 VALUE=value2>
.
.
<PARAM NAME=parameterN VALUE=valueN>
Alternate HTML
</APPLET>

Here is a brief explanation of each of the attributes shown previously:

Required Attributes Valid Values
CODE A valid class file name
WIDTH Width of applet in pixels
HEIGHT Height of applet in pixels
Optional Attributes Valid Values
CODEBASE A valid URL pointing to a directory where the applet’s class files reside.
ALT Alternate text if a Java -enabled browser cannot run the applet.
NAME An instance name for the applet so

other applets on the same HTML page

can communicate with each other.

ALIGN Alignment for the applet. Valid values are left, right, top, texttop, middle, absmiddle,baseline, bottom, absbottom.
VSPACE Spacing (specified in pixels) on the top and bottom sides of the applet.
HSPACE Spacing (specified in pixels) on the left and right sides of the applet.
PARAM Parameters to pass to the applet.

Any HTML text outside the <APPLET> tag, except for the <PARAM> attribute, is ignored by Java-enabled browsers but recognized by all other browsers. So, this is the perfect spot to place some alternative text, such as, “Sorry, you are not running a Java-enabled browser...”, and. perhaps this is a good place to show an image of the applet. Let’s take a look at an example in the next section.
 

Examples of the <APPLET> Tag

Listing 1.2 is an example of how the <APPLET> tag is used by an applet available on the Web, infoBook 2.0, at http://www.patriot.net/users/anil/infoBook/. Notice that several parameters are passed to the applet. These allow the applet to be configured and customized by each site for their parameter’s purpose. Also, notice the “Sorry, you are not...” message between the <H2> and </H2> tags; this part is ignored by Java-enabled browsers but recognized by all other browsers. This difference is a good way to show the users of a non-Java browsers what they are missing out on.

Listing 1.2—HTML for infoBook 2.0.

<APPLET CODE=infoBook.class WIDTH=625 HEIGHT=380>
<H2>Sorry, you are not running a Java enabled browser. If you were, you would be able to run the applet shown in the following image:</H2>
<IMG SRC="infoBook-Config4.gif">
<PARAM NAME="FieldDelimiters" VALUE="|">
<PARAM NAME="MasterDataFile" VALUE="infobook.dat">
<PARAM NAME="MasterListTitle" VALUE="Sample List">
<PARAM NAME="Logo" VALUE="logo.gif">
<PARAM NAME="LogoPos" VALUE="center">
<PARAM NAME="LogoWidth" VALUE="449">
<PARAM NAME="LogoHeight" VALUE="43">
<PARAM NAME="Message1" VALUE="Welcome to infoBook 2.0... ">
<PARAM NAME="Message2" VALUE="A Multi-Purpose Directory Tool... ">
<PARAM NAME="Message3" VALUE="Designed for Internet/Intranet... ">
<PARAM NAME="Message4" VALUE="With Image and Sound Support... ">
<PARAM NAME="Message5" VALUE="eMail and Home Page Hyperlinks... ">
<PARAM NAME="Message6" VALUE="Powerful Search Capabilities... ">
<PARAM NAME="Message7" VALUE="Groups/Departments Supported... ">
<PARAM NAME="Message8" VALUE="Customize With Company Logo and Scrolling Text... ">
<PARAM NAME="NumberOfMessages" VALUE="8">
<PARAM NAME="ScrollSpeed" VALUE="6">
</APPLET>

Listing 1.3 shows another example of the <APPLET> tag by an applet that is available on the Web—Dynamic BillBoard. This example shows an applet in use at the Java Applet Rating Service (JARS) Web site at http://www.jars.com. This applet can display multiple-image files in a bulletin board style that is useful for advertising company slogans and logos.

Listing 1.3—HTML for Dynamic Billboard.

<applet codebase="http://www.jars.com" code="DynamicBillBoard" width="392" height="72">
<param name="delay" value="3000">

<param name="billboards" value="6">
<param name="bill0" value="/images/Jars10.gif,http://205.242.160.203">
<param name="bill1" value="ad_egsoft.gif,http://www.webtrends.com">
<param name="bill2" value="/images/bigbook.gif,http://www.bigbook.com">
<param name="bill3" value="/images/Gb_logo.gif,http://www.webwareonline.com">
<param name="bill4" value="/images/infoBookJARS.gif,http://www.webwareonline.com">
<param name="bill5" value="excite1.gif,http://www.excite.com/search.gw">
<param name="transitions" value="6,ColumnTransition,FadeTransition,TearTransition,SmashTransition,UnrollTransition,RotateTransition">
<a href="http://www.webtrends.com"><img src="ad_egsoft.gif"></a>
</applet>

Real-World Examples of Java Applets on the Web

Because pictures truly say more than a thousand words, you should take a look at some examples of real-world Java applets on the Web today. Figures 1.2–1.11 are examples of these applets. Let’s look at each briefly.

Figure 1.2 shows an applet called WallStreetWeb by BulletProof (www.bulletproof.com/). This applet was one of the earlier client-server applets to show up on the Web and still remains to be one of the more impressive applets out there. This applet shows stock quotes and updated graphs for them, communicates with a back-end database, has a menu bar and much more. It also uses its own Frame which causes it to “pop out” of the Web browser, thereby giving it the look and feel of a GUI application.


FIG. 1.2

WallStreetWeb by BulletProof.

Figure 1.3 shows an Internet Shopping by Eastland Data Systems (www.eastland.com). This is another unique applet, because it implements drag-and-drop features on the Internet. This is one of my personal favorites and a must-see for everyone.


FIG. 1.3

Internet shopping by Eastland.

Figure 1.4 shows infoBook 2.0, an applet that demonstrates many of the features that are available in the Java language, such as multimedia programming, various GUI components, hyperlink capabilities, multithreading, and much more. This applet recently won Gamelan’s “Best of the Year” award, was rated in JARS Top 1 percent, and more. Check it out at www.patriot.net/users/anil/infoBook/.


FIG. 1.4

infoBook 2.0.

Figure 1.5 shows an old and famous video arcade game. For those of you born between the ‘50s and ‘70s like myself, you will probably recognize it. Yes indeed, it’s “Space Invaders”! Developed by Magnastar, Inc. (www.magnastar.com), this applet truly demonstrates the variety of applications you can develop with Java.


FIG. 1.5

Space I by Magnastar Corporation.

JavaGRID by Vincent Engineering (www.vincent.se), shown in figure 1.6, puts business functionality inside a Web browser via Java. This is an example of how a light-weight spreadsheet applet can be used inside of a browser. Check out the Web site (http://www.vincent.se/Products/OCIJavaGateway/nsurldb2.html).


FIG. 1.6

JavaGRID by Vincent Engineering.

Figure 1.7 shows two applets in action at the JARS site (www.jars.com). Both of these applets are true off-the-shelf, light-weight applets that Internet users can plug into their HTML documents.


FIG. 1.7

Dynamic BillBoard and Navigator Ticker at JARS.

The first one, Dynamic BillBoard (http://www.starwave.com/people/robertt), displays GIF files in a sequence with various special effects. This applet is very useful for advertising. The second applet, Navigator Ticker (http://163.121.10.41/java/applets/NavTickr/), displays a number of messages that scroll from right to left. See the previous section “Examples of the <APPLET> Tag” to see how these applets can be configured.

Figure 1.8 is another unique and fun Java applet. This applet keeps the streets clean by allowing the graffiti artists to paint “The Wall” on the screen. Try this applet out at http://militzer.me.tuns.ca/graffiti/graffiti.html.


FIG. 1.8

Graffiti.

As I mentioned in the beginning of this chapter, there are various types of applications that you can develop in Java, packages being one of them. CONNECT! Widgets by ConnectCorp. (www.connectcorp.com) shown in figure 1.9 shows an example of how you can buy off-the-shelf components and plug them into your Java applets and GUI applications.


FIG. 1.9

CONNECT! Widgets by ConnectCorp.

A part of figure 1.10 should look familiar to you. It shows the famous Rubik’s Cube that amused us all a few years ago. This is a fully functional Rubik’s cube developed in Java. You can play with it live on the Internet at www.tdb.uu.se/~karl/java/rubik.html.


FIG. 1.10

Rubik’s Cube.

Figure 1.11 was something I developed to try out the various features of java.awt, java.net, and java.io packages. It is shown here because this applet, Text Editor, can also be used as a GUI application (see the next section, “Java GUI Applications”). The applet and its source code can be viewed at www.patriot.net/users/anil/java/TextEditor/.


FIG. 1.11

Text Editor applet.

Java GUI Applications

While Java applets have stolen most of Java’s thunder, Java goes a lot further than applets. Java can be used to develop portable GUI applications across all supported platforms. In fact, the same Java source code can be used for both an applet and an application.

To illustrate this, let’s look at an application, Text Editor, that was developed for demonstration purposes. As the name implies, this application is used for editing text files, similar to the Windows Notepad application. Figure 1.11 shows the applet version of the Text Editor, figure 1.12 shows the application version on Windows 95, and figure 1.13 shows the application version under Solaris.

All three versions of the Text Editor were generated using the same Java source files. In fact, all three versions are executed using the same exact bytecode files that were compiled only once under Windows 95 and copied over to Solaris without requiring a recompilation.


FIG. 1.12

Text Editor application on Windows 95 run using the Java interpreter. Notice how the Java interpreter is used on the MS-DOS prompt to execute the application.


FIG. 1.13

Text Editor application on Solaris.

Notice the File dialog box in figures 1.12 and 1.13. If you are a Windows 95 or Solaris user, you know they are the standard file dialog boxes that are used on these operating systems. As a developer, you do not need to custom code anything to get the native look and feel. All you have to do is ensure the class (bytecode) files are available from where the applet or application needs to be invoked. The rest (the native look and feel, system specific features, and so on) is handled by Java’s dynamic link libraries.

Java Command Line Applications

Even in today’s world, where GUI applications have become a standard on practically every type of computer, there are times when you might need to drop down to the command line to perform some tasks. For times like these, Java provides the ability to develop command line applications.

The only difference between command line and GUI applications is that command line applications do not use any of the GUI features that are provided in Java. In other words, command line applications do not use the java.awt package.

Figure 1.14 shows an example of a command line application, copyURL, which essentially is a copy utility for copying files from the Internet to a local disk. This application uses the java.net package to obtain information about a resource (file) on the Internet. Then copyURL uses the java.io package to read the bytes of data from the file on the Internet and write them to a local file.


FIG. 1.14

copyURL.class is an Internet command line copy utility.

Java Is Client-Server!

In today’s computing world, client-server technology has found a place in most corporations. The biggest benefit of this technology is that the processing load is shared between the client and the server. A client can be any program (GUI application, Telnet, and so on) that requests services from a server application. Examples of server applications include database servers, application servers, communication (FTP, Telnet, Web) servers, and more.

So far in this chapter, you have seen several examples of Java client-side applets and applications. However, Java provides classes for server-side processing as well. Java applications can be used as clients or servers, whereas applets can only be used for client-side processing.

The java.net package provides classes necessary for developing client-server applications. Figure 1.15 shows a Java applet, javaSQL, that sends free-form SQL queries typed in by the user to a server application, javaSQLd. javaSQLd in turn queries a database and returns the query results to the javaSQL applet.


FIG. 1.15

javaSQL client applet.

Figure 1.16 illustrates the relationship between javaSQL and javaSQLd. Imagine querying a database at work from home via a Java-enabled browser. With Java, the possibilities are endless!


FIG. 1.16

javaSQL and javaSQLd.

How to Stay Current

The Web-related technology is progressing so rapidly that it is difficult to stay on top of new events. In fact, a friend of mine made a joke once by saying that technology is progressing so rapidly nowadays that if you have a slow modem, you are likely to fall behind.

One way to stay current is to visit certain Web sites that post the latest news and Java examples. While there are several Java-related Web sites out there, here are the most well -known ones. Those listed generally provide all of the necessary information and contain links to other not so well-known sites:

Web Site URL
Gamelan http://www.gamelan.com
Java Appplet Rating Service http://www.jars.com
Sun’s Java site http://www.javasoft.com
JavaWorld Magazine http://www.javaworld.com
Digital Focus http://www.digitalfocus.com
WebWare Online http://www.webwareonline.com
Team Java http://www.teamjava.com


Previous PageTOCNext Page

| Previous Chapter | Next Chapter |

| Table of Contents | Book Home Page |

| Que Home Page | Digital Bookshelf | Disclaimer |


To order books from QUE, call us at 800-716-0044 or 317-361-5400.

For comments or technical support for our books and software, select Talk to Us.

© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.