Section 23.3. Using the Java Plug-in


23.3. Using the Java Plug-in

The disadvantage of the <applet> tag is that you generally have to rely on the browser's Java interpreter. This is bad for two reasons:

  • The version of Java that is included in popular browsers lags the current version of Java by years and Java has never been fully supported in Internet Explorer (IE).

  • Historically, Microsoft's version of Java has had its own bugs and idiosyncrasies, which were different from the bugs and idiosyncrasies of Sun's version. Microsoft is also in the process of dropping its support for Java in general, so it is unclear what the future holds for IE.

At the time of this writing, most common versions of Netscape and IE are stuck with a Java 1.1 environment. Modern versions of the Netscape and Firefox web browsers as well as Mac OS X's Safari support current versions of Java when they are installed. However, Mac OS X will not support Java 5.0 until its 10.4 release. As a developer, you will want to use modern features of Java including Swing, Collections, etc., on all platforms. So what can we do?

23.3.1. What Is the Java Plug-in?

We might take the state of Java in the browser to indicate that applets are dead. But the Java Plug-in might just save the day. A plug-in is simply a loadable application module that is used to support new content types in a web browser. All major browsers have a plug-in mechanism that allows the browser to be extended in this way.

You're probably familiar with plug-ins of one type or another. The browser gives control for a part of a web page to another piece of code. For example, you can view movies in your browser using Apple's QuickTime plug-in. You can view interactive multimedia with Macromedia's Shockwave plug-in. The idea is very similar to applets; basically the browser hands-off responsibility for some rectangular area on the screen to someone else's code. The Java Plug-in is simply a Java runtime environment implemented as a browser plug-in.

Applets that use the Java Plug-in can take advantage of the very latest Java platform features. With the Plug-in, Java developers can specify the version of Java they require, and their applets should run in exactly the same way in any browser using it. The browser isn't even really running the applet anymore; the Plug-in takes care of it.

This is nifty technology, but it does come with a small hurdle for deployment. Users who want to use the Java Plug-in to run applets have to download and install it first. While this is not a huge burden, it is a barrier to universal acceptance. With Java 5.0, the Java Plug-in is installed automatically when the Java Runtime Environment (JRE) is installed, so users have one-stop shopping for Java. But with the wide installed base of browser versions by both Microsoft and Netscape, installation will be an issue for some time to come.

23.3.2. Messy Tags

The HTML for web pages that contain Plug-in applets is much messier than the <applet> tag you've already seen. Part of the problem is that you have to use specialized tags for different browsers (primarily IE). Netscape, Firefox, and Safari use the <embed> tag for plug-ins while IE uses the <object> tag. However, there are some tricks involving fitting the <embed> tag inside the <object> tag that should allow all browsers to recognize and run the same document correctly.

These tags have their own syntax, but, basically, you're still providing the same information to the browser. You specify the size of the applet, the class name to use, the location of additional classes, and parameters that should be passed to the applet itself. Fortunately, you don't have to worry too much about the details. Sun provides a handy utility, the HTML Converter, which converts <applet> tags to the appropriate <embed> and <object> tags. Assuming you've set up your <applet> tag correctly, you should have no trouble converting your HTML page to use the Java Plug-in. This utility is available for download at http://java.sun.com/products/plugin/.

Suppose, for example, that you create a web page called ShowOff.html. Once you have the <applet> tag set up the way you want (you can test it with a reliable browser or appletviewer), you can use the HTML Converter to set up your web page to use the Plug-in. The HTML Converter runs as a Swing application. To run it, navigate to the directory where you installed the HTML Converter and type the following:

     C:\> java HTMLConverter 

The utility is pretty self-explanatory. You can convert all HTML files in a directory or just convert a single HTML file. The conversion is done "in-place," which means that your original HTML is overwritten with the new stuff. The Converter automatically backs up your old files unless you tell it otherwise.

You can perform different kinds of conversions, represented by different templates. By default, the HTML Converter uses the "standard" template that produces a page that works with Netscape and Internet Explorer on Windows and Solaris. If you choose "extended," it tries to produce a more elaborate version that should work on a wider set of browsers. In addition to adding the <embed> and <object> tags, the "extended" template preserves the original <applet> tag. This has the added benefit that you'll still be able to test your applet with appletviewer.

23.3.2.1 Applets in Internet Explorer

We should also note that as of Version 1.4, the Java Plug-in for Windows has a new feature: once installed, the Plug-in takes over control of the regular <applet> for IE so that, by default, all applets begin to use the Plug-in. Once the Plug-in installation is boot-strapped, all applets benefit from it, regardless of whether the page uses the special tags. This is why if, after installing the Java Plug-in, you run the ShowApplet from our earlier example, it should show the latest Java version even without using special markup. The main advantage of the HTML Converter in this case is for users who have not yet installed the plug-in. In that case, the special HTML instructs them that they need to install it, instead of simply running the degenerate version of Java.

23.3.3. Viewing Plug-in Applets

What actually happens when users browse to a page with HTML that requires the Plug-in for an applet? It depends, of course, on which browser you're using and what has been installed. If you are on a "virgin" system, IE asks the user for permission to download and run the Java Plug-in to display the applet. Older versions of Netscape direct the user to the JRE download page. (The Java Plug-in is distributed as part of the JRE.) In either case, the download and installation times vary. Depending on network speed and user comfort level, you can expect that most Internet users will have to spend five to ten minutes downloading and installing the JRE.

The good news is that the installation needs to be done only once. When JRE is installed and running, you can view Plug-in applets immediately (at least as soon as the browser loads the Plug-in). The only time the user needs to install a new Plug-in is when a new version of Java comes out. The current version of the Plug-in can automatically check for updates on a periodic basis, like most modern software.

Despite the price of installation and HTML tag messiness, the Plug-in is powerful medicine indeed. No longer do you have to wait for browser vendors to implement the latest Java platform features; the Plug-in makes the latest Java releases available to applets immediately, and it provides a more hospitable environment for applets, regardless of what browser displays them.



    Learning Java
    Learning Java
    ISBN: 0596008732
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net