by Anil Hemrajani with Joe Weber
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 inventors of Java admit that it borrows heavily from previous languages, such as C, C++, Eiffel, SmallTalk, Objective C, and Cedar/Mes. So it is not surprising that when using Java you can accomplish a lot of the same tasks that traditional languages can achieve. For example, you can use C++ to build command line utilities, class libraries, GUI applications, and more. Java is no different. The following are the four types of applications you can build in Java:
Applets, the first type, are essentially applications that run inside a Java-enabled browser, such as Netscape v2.0 and higher, Microsoft Internet Explorer v3.0 and higher, or HotJava.
The second type is a 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 clas-ses belonging to 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).
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 buzzwords fit the bill, so they are used here to describe Java. According to Sun:
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 buzzword you need to touch for this chapter 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 reason Java became so popular was not only because of the benefits of the language itself. 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.applet | Classes for developing applets. |
java.awt | Abstract Window Toolkit (AWT) classes for the GUI interface such as Windows, dialogs, buttons, text fields, and more. |
java.net | Classes for networking, URLs, client-server sockets. |
java.io | Classes for various types of input and output. |
java.lang | Classes for various data types, running processes, strings, threads, and much more. |
java.util | Utility classes for dates, vectors, and more. |
java.awt.image | Classes for managing and manipulating images. |
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 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.
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:
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.
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.
<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. |
NOTE: 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. Take a look at an example in the next section.
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.chipsoftinc.com/products/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.
<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 with 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.
<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>
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. Look at each briefly.
Figure 1.2 shows a Java application called NetProphet. NetProphet is a wonderful
utility that allows you to chart and graph all of your stocks. It is a wonderful
example of how having a Java client interacting with a server (client-server) can
be used to create dynamic information. Netprofit is available from Neural Applications
at http://www.neural.com/NetProphet/NetProphet.html.
FIG. 1.2
NetProphet is a wonderful example of client-server java.
Figure 1.3 shows an Internet Shopping page by Eastland Data Systems (www.eastland.com/shopping).
This is another unique applet because it implements drag-and-drop features on the
Internet. This is a personal favorite 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. Check
it out at http://www.
chipsoftinc.com/products/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/applets/games/space),
this applet truly demonstrates the variety of applications you can develop with Java.
FIG. 1.5
Space Invaders 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 at http://www.
vincent.se/Products/OCIJavaGateway/nsurldb2.html.
FIG. 1.6
JavaGRID by Vincent Engineering.
Figure 1.7 shows a really nifty applet designed by Netscape to demonstrate its
IFC technology. The applet is called Aquarium, and it features a drag-and-drop interface.
Simply drag fish, sea weed or bubbles into the fish tank. All the windows can be
dragged around too. When you're done, hit the "Supply Invoice" rock to
get your bill. Talk about interactive shopping! (http://developer.netscape.com/library/ifc/examples/Aquarium/Aquarium.html)
FIG. 1.7
Aquarium is a drag-and-drop shopping cart application with a totally new twist.
FIG. 1.8
Graffiti.
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/.
As was 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 everyone 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 this author 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 http://www.chipsoftinc.com/products/netEdit/.
FIG. 1.11
Text Editor applet.
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, look at an application called 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.
FIG. 1.12
Text Editor application on Windows 95 run using the Java interpreter.
FIG. 1.13
Text Editor application on 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 bytecode files that were compiled only once under Windows 95 and copied over to Solaris without requiring a recompilation.
Notice how the Java interpreter is used on the MS-DOS prompt to execute the application.
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 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.
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 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.
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. The pace of innovation on the Internet is extremely rapid, and Java has been growing at a rate at least as astounding as the rest of the technology.
About the only way to stay current is to visit certain Web sites that post the
latest news and Java examples. While there are dozens of Java-related Web sites that
provide timely information, here are a list of some of those that have a history
of providing great information:
Web Site | URL |
Sun's Java site | http://www.javasoft.com |
Javology Magazine | http://www.javology.com |
JavaWorld Magazine | http://www.javaworld.com |
Java Applet Review System | http://www.jars.com |
Team Java | http://www.teamjava.com |
Gamelan | http://www.gamelan.com |
WebWare Online | http://www.webwareonline..com |