Chapter 19

Adding Java to Your Web Site


CONTENTS


Java is a platform-neutral programming language developed for the Internet by Sun Microsystems. Unlike other programming languages, applications created with Java work on any computer platform as long as an applet viewer is available for that platform.

Except for a few differences, Java is a programming language that is similar to C++. Therefore, comprehensively covering programming with Java is beyond the scope of this book. This chapter will demonstrate how you can add Java applets to your Web site and introduce you to Java programming by demonstrating how to write a simple Java applet. The first few sections will discuss various capabilities and applications of Java. Afterwards, you will be shown how Java applets can be downloaded and added to your Web site. The next few sections briefly cover Java programming fundamentals and demonstrate how to create a simple Java applet. You also will be shown how to download and install the Java developer's kit so you can start experimenting with creating Java applets. The final section of this chapter lists various Java resources on the Internet. Visit Web sites listed in this section to obtain more information about Java and find interesting Java applets that can be added to your Web site.

Benefits of Java

There are numerous benefits in using Java to make a Web site more interesting to navigate. Unlike some other technologies that need a special plug-in or helper application, Java applications are executed on the browser. This is convenient for users, because all they have to do is use a Java-compatible Web browser.

Java is a secure programming language. This is an important aspect of Java. A Java applet in a Web page is essentially an application that can be downloaded from anywhere on the Internet and executed on a user's Web browser. The Java language has been implemented with security in mind to make sure Java applets cannot be used for malicious intent (such as to format a user's hard drive). Computer security is never perfect. Sooner or later someone with a lot of free time is going to discover a way to defeat various security mechanisms that have been put in place to enforce security. However, Java is reasonably safe to be used on the Internet and the Internet community has accepted it as a reasonably secure programming language. Browse the Java security FAQ to obtain more information about Java security.

URL
The Java security FAQ:
http://java.sun.com/sfaq/

Drawbacks of Java

At the moment, most Java applets are nothing more than eye candy for Web pages. However, this is likely to change in the near future as Java developers realize various capabilities of Java and begin creating useful applications that solve real-world tasks. FutureTense Texture, covered in Chapter 14, is an excellent example of such an application. Although Java is a powerful language, Java applications are somewhat plagued with bandwidth limitations. This is holding back some of its potential, because it's often hard to write sophisticated applications that can be transmitted through a slow modem link in a few seconds. As high bandwidth connection lines find their way to more users, Java developers will be able to create more sophisticated applications that perform various useful real-world tasks.

How Does Java Work?

Java is similar to traditional object-oriented programming languages except for one difference. Usually when a program is compiled, an executable binary file is created. This file can be executed on the computer the binary file was created. On the other hand, when Java source code is compiled, a Java bytecode file is created. Bytecode is an intermediary state between source code and binary executable code. Although bytecode is slower than binary executable code-because it has to go through another layer of processing before it can be executed-Java bytecode is platform neutral. This is a major advantage for using Java to create applications for the Web, because the Internet is made up of many different kinds of computers. Figure 19.1 illustrates how Java applets are created. As shown in Figure 19.1, Java source code can be converted into platform neutral bytecode using a Java compiler. Due to this reason, Java applets run on a variety of operating systems including Windows NT, Windows 95, Macintosh, and various flavors of UNIX.

Figure 19.1: How Java applets are created.

Java applets in Web pages are downloaded and executed by Web browsers that support Java. This process is shown in Figure 19.2. After a Web browser downloads a Java applet, it is executed using a Java bytecode interpreter.

Figure 19.2: How Java applets are executed.

Adding Java Applets to Your Web Site

Adding a Java applet to a Web page is as easy as adding an inline image to a Web page. The only difference is rather than specifying the URL of an image, you will be specifying the URL of a Java applet. Before adding Java to a Web site, it helps to know various parameters that can be used to insert Java applets to a Web page. The following HTML tags can be used to insert a Java applet into a Web page and pass information to it using <param> tags. Note that data fields enclosed in two square brackets should be replaced with values specific to the applet being inserted.

applet
  code=["JavaApplet.class"]
  width=[Width of applet] height=[Height of applet]
  alt=["Description of Java applet"]
  align=["alignment of applet"]
  vspace=[vertical space around applet]
  hspace=[horizontal space around applet]
  codebase=["/Java/AppletDirectory"]>
<param name=[ParameterName] value=[ParameterValue]>
</applet>

When adding a Java applet to a Web page code, width, and height are required HTML tags. Refer to Table 19.1 for a list of applet definition parameters.

Table 19.1. Java applet definition parameters.

HTML TagDescription
code Filename of Java applet. The Java applet's location will be calculated relative to the codebase element if it is specified in the applet definition.
Width Width of applet window in number of pixels.
Height Height of applet window in number of pixels.
Alt Text displayed by Web browsers that do not support Java.
Align Alignment of Java applet. This parameter is used the same way the align attribute is used in the <IMG> tag.
vspace Vertical space around the applet.
hspace Horizontal space around the applet.
Codebase URL of directory containing the Java applet's code.

Adding an LED Message Display

A Java applet that can be used to display messages in an LED display has been written by Darrick Brown. This applet can be downloaded from the URL listed next. After downloading it, decompress the distribution file using a file decompression utility such as WinZip.

URL
The LED sign Java applet:
http://www.cs.hope.edu/~dbrown/java/LEDSign/WWW/LED.html

A directory by the name of LEDSign will be created when the distribution file is decompressed. It is recommended that you move this entire directory to the root directory of your Web server. Or, if you would like to keep things more organized, create a directory under your Web server's root directory called Java. You then can use this directory to store Java applets and copy the LEDSign directory structure into the Java directory that you just created. Afterwards, open a Web page where you would like to insert the Java applet, and insert the following code where you would like the Java applet to appear. The following example assumes you copied the LEDSign directory structure into the root directory of your Web server. After the following code is inserted into a Web page and it is viewed with a Java-compatible Web browser, the LED sign applet will be visible in the Web page as shown in Figure 19.3. Note that you do not have to have a Web server installed to view Java applets using a Web browser. If a Java applet and its corresponding HTML file is in the same directory, simply open the HTML file locally and you will be able to see the Java applet.

Figure 19.3: LED sign Java applet.

<applet codebase="/LEDSign/LED" code="LED.class"
  width=405 height=40 >
   <param name="script" value="/LEDSign/scripts/Demo.led">
   <param name="border" value="1">
   <param name="bordercolor" value="100,130,130">
   <param name="spacewidth" value="3">
   <param name="wth" value="100">
   <param name="font" value="/LEDSign/fonts/default.font">
</applet>

Adding a Digital Java Clock to a Web Page

John Criswick has created a digital Java clock that can be inserted into a Web page to display the current time. It is easy to download and insert this Java applet into a Web page.

URL
The digital clock can be downloaded from the Java LED clock Web site:
http://www.conveyor.com/digital-clock.html

After visiting the digital clock Web site, you might want to download the raw Java class file and save it into a local directory rather than downloading a compressed version of the Java applet. The applet is so small that its not worth the trouble of downloading a compressed version and decompressing it using another program. After downloading the class file, save it into a local directory. If you are adding the digital clock into a page at your Web site, you might want to create a directory called /Java/Classes and store all your Java class files in that directory. Assuming you downloaded the class file into the /Java/classes directory, the following HTML code can be used to add a digital Java clock to a Web page. Additional configuration information pertaining to the digital clock applet can be obtained from the same Web site from which you downloaded the Java applet. The Web page in Figure 19.4 uses the following HTML code to display the digital Java clock:

Figure 19.4: Digital clock Java applet.

<applet codebase="/Java/Classes/"
   code="BigTime10b2.class" width=120 height=36>
   <param name=ledcolor value=green>
   <param name=framecolor value=lightgray>
   <param name=backcolor value=black>
   <param name=mode value=12>
</applet>

As shown in the previous two examples, it is easy to add Java applets to a Web page. Visit Yahoo!'s Java information Web page (the URL is given at the end of this chapter) to locate and download useful Java applets. When adding Java applets, be considerate toward users browsing your Web with relatively slow POTS connections to the Internet. As a rule of thumb, avoid using Java applets that take longer than 30 seconds to load over a relatively slow POTS link to the Internet.

Installing the Java Developer's Kit

The Java developer's kit has to be installed from the CD-ROM that came with this book before it can be used to develop Java applications.

To install the Java SDK, copy it to the parent directory of the directory in which you plan to install the Java developer's kit. For example, if you would like to install the Java developer's kit in the directory K:\Internet\Authoring\Java, copy the Java developer's kit distribution file to the K:\Internet\Authoring directory. Afterwards, simply execute the distribution file and it will create a directory called Java and install the developer's kit into this directory.

Afterwards you need to set two Windows NT environment variables. To do this, invoke Control panel and double-click the System icon. This will bring up the system configuration dialog box shown in Figure 19.5. This dialog box can be used to create two required Windows NT environment variables as shown in Figure 19.5.

Figure 19.5: Windows NT System configuration dialog box can be used to add and modify NT environment variables.

Update the PATH variable to contain the location of your java\bin directory. For example, if you installed the Java developer's kit into the K:\Internet\Authoring\Java directory, add K:\Internet\Authoring\Java\bin to the PATH variable. Note that semicolons are used to separate directories in the PATH variable.

After updating the PATH variable, a new environment variable called CLASSPATH has to be created. The value of CLASSPATH should point to the java\classes directory. For example, if the Java developer's kit was installed into the K:\Internet\Authoring\Java directory, assign the value K:\Internet\Authoring\Java\classes to the CLASSPATH variable as shown in Figure 19.5. Note that you need to log out and log back in for the environment variable changes to take effect.

Creating a Simple Java Applet

It is easy to create Java applets using the Java developer's kit. If you are already familiar with C++ or C, Java programs will look somewhat familiar to you. You might want to obtain a copy of Teach Yourself Java in 21 Days by Sams.net publishing to learn more about creating Java applications. It is an excellent book and has been written so that even if you do not have a background in C or C++, you can still follow the text and learn how to create Java applications. The following example is meant to get you started with creating Java applets and demonstrate how a Java application can be developed and compiled using the Windows NT Java developer's kit.

Browse the Java resources in the next section to find out more about the Java language and how it can be used to create applications for the Internet. The following Java program can be used to display the string "Hello World." When naming Java applications, use the applet's class name and add a .java extension to it. For example, the following application will be saved as HelloWorld.java.

// Java Hello World Application (HelloWorld.java)

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class HelloWorld extends java.applet.Applet {

  Font textFont = new Font ("TimesRoman",Font.BOLD,48) ;

  public void paint(Graphics g) {
    g.setColor      (Color.blue) ;
    g.setFont       (textFont) ;
    g.drawString ("Hello world!", 10,60) ;
  }

}

The Hello World application can be compiled using the command javac HelloWorld.java. This command invokes the Java compiler, checks the file HelloWorld.java for any syntax errors, and creates a bytecode file with the extension .class. Upon successful compilation of the Hello World application, the Java compiler will create a bytecode file named HelloWorld.class. The following HTML code can be used to insert the Hello World Java applet into a Web page. The Hello World Java applet created in this exercise is shown in Figure 19.6.

Figure 19.6: Java Hello World application.

<applet
  code="/Java/Classes/HelloWorld.class"
  width=300 height=100>
</applet>

Java Resources on the Internet

Numerous Java resources are available on the Internet. Visit the following Web sites to locate useful Java applications you can add to your Web site. By visiting these Web sites, you also will be able to learn how to write Java applications by examining the source code of applications and tutorials.

Sun Microsystems's Java Overview Web Page

Visit Sun Microsystems's Java overview Web page to obtain official information about Java. This Web page contains many white papers and information that is useful for creating Java applets.

URL
Sun Microsystems's Java overview Web page:
http://java.sun.com/doc/overviews.html

The Java Tutorial Web Page

The Java Tutorial Web page provides a quick overview of the Java language specification and demonstrates how Java can be used to create applications for the Internet. Visit this URL to learn how to write programs with Java by experimenting with source code samples found at this Web site.

URL
The Java Tutorial Web page:
http://java.sun.com/tutorial/

EarthWeb's Gamelan

Visit EarthWeb's Gamelan Web site to find out how Java can be used to make a Web site more interesting to navigate. This Web site also contains many Java resources for developing Java applications. Many Java programming resources also can be found at this Web site.

URL
EarthWeb's Gamelan Web site:
http://www.gamelan.com/

Yahoo!'s Java Information Web Page

Yahoo!'s Java information Web page is a comprehensive source for locating Java-related Web sites on the Internet. It is highly recommended that you add this site to your Internet bookmark list and visit it periodically to find out new and innovative ways of using Java to make Web pages interactive and more interesting to navigate.

URL
Yahoo!'s Java information Web site:
http://www.yahoo.com/Computers_and_Internet/Languages/Java/

Java Mail List Archives

When learning a new language, it is often helpful to learn about problems and solutions encountered by others. Visit the Java mail list archives for issues related to Java encountered by other Java developers. By browsing this archive, you will not only be able to find problems encountered by other Java developers, but also solutions to those problems recommended by others.

URL
The Java mail list archives:
http://java.sun.com/archives/

Java Newsgroup

If you have a question about using Java, you should visit the Java newsgroup comp.lang.java. Many Java programmers read this newsgroup frequently to discuss issues related to creating Java applets.

Summary

Java is a powerful programming language that can be used to create applications for the Web. Java is an ideal programming language for the Internet because it can be used to create applications that are platform neutral and secure. In fact, FutureTense Texture, covered in Chapter 14, actually relies on a Java applet to format objects in a Web page.

It is easy to add Java applets to a Web page. In fact, it's as easy as adding an inline image to a Web page. Java applets can be used to make Web pages interactive and more interesting to browse.

The Java developer's kit can be used to create Java applets. It can be downloaded free of charge from Sun Microsystems's Web site. The Java developer's kit can be used to compile Java source code files into Java bytecode files. Java bytecode files are platform neutral and can be viewed on any platform supported by Java.

What's Next?

Prior to client-side scripting languages, making a Web page interactive meant using a CGI program along with HTML input controls. Although this is quite effective in some cases, it is a waste of network bandwidth, time, and server processing power. This created the phenomenon known as the thin client syndrome. Thin client syndrome refers to Pentium and 486 computers used to browse the Web, essentially doing nothing more than fetching an HTML page from a Web server and displaying it. After the page is displayed, its CPU is idle, doing nothing, in most cases. This is a waste of client-side resources.

JavaScript is a powerful scripting language that can be used to combat the thin client syndrome and create interactive Web pages. Because JavaScript applications do not need to interact with a Web server, they are capable of validating information users enter into various forms before the data is sent to a Web browser for processing. This saves time, resources of the Web server, and also network bandwidth. JavaScript also can be used to make a Web site easier to navigate by providing additional information to users browsing a Web site. For example, when a user moves the mouse pointer over an HTML link, JavaScript can be used to display a message on the status bar of the Web browser and give more information about the link.