Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

The Java Plug-in

Because there are now five major versions of the Java language, one of the things you must concern yourself with as an applet programmer is the version your audience can handle.

None of the leading Web browsers offer built-in support for Java 2, and that doesn't seem likely to change. Netscape Navigator, Microsoft Internet Explorer, and Opera include Java interpreters that support Java 1.0 fully and some of the features of Java 1.1.

Applet programmers who want their programs to run on Navigator and Internet Explorer can use Java 1.0. This provides the widest possible audience, but Java 1.0 is a much simpler language than Java 2, lacking many of the best features of the language, including Swing, JavaBeans, improved event-handling, JDBC, and other changes that reflect three years of feedback from Java developers.

To make it possible for Java 2 applets to be written for current browsers, Sun Microsystems offers the Java Plug-in, a substitute for each browser's built-in interpreter.

A plug-in is a program that works in conjunction with a Web browser to expand its functionality. Plug-ins handle a type of data that the browser normally could not handle. For example, Apple offers a plug-in to display QuickTime movies and Macromedia has released a plug-in to run Flash animation files.

If you installed the Software Development Kit, you were given a chance to install the Java Plug-in at the same time.

The Java Plug-in runs Java applets in place of the Web browser's Java interpreter. Once the Java Plug-in is installed, all future Java 2 applets will run automatically if they specify that the Plug-in should be used to run them.

The plug-in is part of the Java Runtime Environment, which can be downloaded at no cost from Sun's Java site at http://java.sun.com/getjava/.

The HTML tags you have learned about up to this point do not specify that the Java Plug-in be used to run applets. Browsers that are not equipped with the Java Plug-in will attempt to run the applet with their own built-in Java interpreters and fail, displaying an error message in the browser's status line.

To make an applet run on the Java Plug-in, you can use a different HTML tag to load the applet: <OBJECT>.

The <OBJECT> Tag

The <OBJECT> tag is used for all objects that can run as part of a Web page, including applets and other types of interactive programs. It is supported by versions 4.0 and higher of Netscape Navigator, Microsoft Internet Explorer, and the SDK appletviewer.

The tag uses several of the same attributes as the <APPLET> tag, including WIDTH, HEIGHT, ALIGN, HSPACE, and VSPACE, but not all of them. One that's missing is the CODE attribute. Instead, the <OBJECT> tag requires a new <PARAM> tag that takes the following format:

<PARAM NAME="Code" VALUE="AppletClassname"> 

If the name of your applet's main class file is Zork.class, the tag would be the following:

<PARAM NAME="Code" VALUE="Zork.class"> 

The <PARAM> tag is used to specify parameters, which are the applet's version of command-line arguments. You can have several <PARAM> tags within an applet, and all of them must be located between the beginning <OBJECT> tag and the ending </OBJECT> tag.

The <OBJECT> tag also requires the classid attribute, which identifies the Java Plug-in as the interpreter that should be used to run the applet. This attribute should always have the following value:

clsid:8AD9C840-044E-11D1-B3E9-00805F499D93 

This value is a string that identifies the Java Plug-in. You'll learn more about how to use it later today.

The <OBJECT> tag also has a CODEBASE attribute that contains a Web page address for the Java Plug-in. For the current version of Java 2 (version 1.4), the CODEBASE attribute should have this value:

http://java.sun.com/products/plugin/autodl/jinstall-1_4_0-win.cab

Listing 17.3 contains a Web page that uses the <OBJECT> tag to load an applet.

Example 17.3. The Full Text of SalutonObject.html

 1: <html> 
 2: <head> 
 3: <title>Saluton Mondo!</title> 
 4: </head> 
 5: <body bgcolor="#000000" text="#FF00FF"> 
 6: <center> 
 7: This a Java applet:<br> 
 8: <object height=150 width=300 
 9: classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" 
10: codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4_0-win.cab"> 
11:     <param name="Code" value="SalutonApplet.class"> 
12: You need a Java-enabled browser to see this. 
13: </object> 
14: </body> 
15: </html> 

If you load this page with a browser that supports the <OBJECT> tag, two things might happen:

After you have installed the Java Plug-in, it runs all applets as if it was the browser's built-in Java interpreter.

Share ThisShare This

Informit Network