Using the Java Plug-in

The disadvantage of the <APPLET> tag is that you 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 the SDK. It was a painfully long time, for instance, between the release of SDK 1.1 and the time that SDK 1.1 was supported in Netscape and Internet Explorer. As a matter of fact, it hasn’t ever been fully supported in Internet Explorer—Microsoft has its own agenda, which may not match up with your “write once, run anywhere” aspirations.

  • The browser’s Java implementation has its own bugs and idiosyncrasies, which are different from the bugs and idiosyncrasies of Sun’s SDK. If you are hoping to run your applet on both Netscape and Internet Explorer, you will have to test it on both platforms to make sure you don’t run into any trouble. This is tedious and frustrating.

At the time of this writing, both browsers are moderately stable for SDK 1.1 development. As a developer, though, you’re probably itching to use all of those cool Java 2 features. What can you do? In light of the lawsuits brought by Sun and the Department of Justice, it is unlikely that Microsoft will ever support Java 2 in Internet Explorer. Netscape’s Java support in Navigator is likewise questionable, though for different reasons. In the foreseeable future, you can’t rely on Java 2 support in Navigator or Internet Explorer.

What Is the Java Plug-in?

Does that mean applets are dead? Heck no! A clever technology called the Java Plug-in saves the day. Both Navigator and Internet Explorer have a plug-in mechanism that allows the browser to be extended to handle new types of data.[52] 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 strikingly 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. Furthermore, applets should run exactly the same in any browser. 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 at a price. Users who want to use the Java Plug-in to run applets will have to download and install it first. While this is not a huge deal, it is a barrier to universal acceptance. (It’s conceivable that America Online, which owns Netscape now, will package the Plug-in with future versions of Navigator. But it’s unlikely that Microsoft would make the same move with Internet Explorer.)

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 separate tags for Internet Explorer and Navigator. Navigator recognizes the <EMBED> tag, while Internet Explorer recognizes the <OBJECT> tag. If you are clever about fitting the <EMBED> tag inside the <OBJECT> tag, you’ll end up with some HTML that both browsers will recognize and run correctly.

These tags have their own little 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, where to find 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 , that 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/1.2/converter.html.

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 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 window 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 will automatically back 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 a template that produces a page that will work with Navigator and Internet Explorer. If you want it to work with other browsers that support Java 2, you’ll need to specify the “Extended” template rather than the “Standard” template.[53] In addition to adding the <EMBED> and <OBJECT> tags, the “Extended” template will preserve the original <APPLET> tag. This has the added benefit that you’ll still be able to test your applet with appletviewer.

Viewing Plug-in Applets

What actually happens when users browse to a page with a Plug-in applet? It depends, of course, on which browser you’re using. Internet Explorer asks the user for permission to download and run the Java Plug-in in order to display the applet. Netscape Navigator directs the user to the Java Runtime Environment (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 expertise. You can expect that most Internet users will have to spend ten or more minutes downloading and installing the JRE.

The good news is that the installation only needs to be done once. Once you have JRE installed and running, you can view Plug-in applets immediately (at least as soon as the browser loads the plug-in). The only time you’ll need to install a new Plug-in is when a new version of Java comes out.

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.



[52] Microsoft calls this technique ActiveX custom controls. But it’s exactly the same concept: the browser gives control for some part of a web page to another piece of code.

[53] At this writing, no browsers support Java 2. But it seems likely that Sun will release a new version of HotJava soon.

Get Learning Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.