Near the beginning of the notes, you may recall a discussion about the Java Plug-in. Here is a recap: if you decide to write an applet that will require the Java Plug-in, to allow older browsers to run your applet, your HTML file needs to be rewritten (If this doesn't sound at all familiar, you can read it here). The Java Plug-in HTML converter conveniently generates the appropriate tags for you. If you are a purist, or just do not have any more room on your hard drive for handy time-saving tools, you can do the conversion yourself. Let us look at how to do it with an example.

Suppose that you have written an applet, and your tags to include it from the HTML page look like:

<applet code="myApplet.class" codebase="./classFiles/" width=50 height=50>
    <param name="animal" value="fish">
</applet>

Remember that codebase specifies the directory containing your code. Also, notice that we have a parameter (animal) with a set value (fish).

If life was fair, all browsers would load the Java Plug-in with the same set of tags. But life isn't fair, which means you need to write different tags for different browsers. In the interest of time, we are only going to look at how to rewrite the tags for two popular browsers: Internet Explorer and Netscape Navigator.

Here is how you would rewrite your HTML for Internet Explorer:

<object
       classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
       codebase="http://java.sun.com/products/plugin/1.1/jinstall-11-win32.cab#Version=1,3,0,0"
       width=50 height=50>
    <param name="code" value="AppletThatRequiresPlugin.class">
    <param name="codebase" value="./classFiles/">
    <param name="animal" value="fish">
</object>
The classid attribute looks slightly alarming, but Microsoft isn't trying to drive you insane (this time). This long string (8AD9C840-044E-11D1-B3E9-00805F499D93) is the unique identification for the ActiveX control more commonly known as the Java Plug-in. The good news is that you just need to cut and paste it into your HTML file.

Next we have the codebase attribute, which is different from the codebase attribute in your original HTML with the <applet> tag. This codebase attribute specifies the location of the Java Plug-in JRE (Java Runtime Environment). If the JRE isn't already on the user's computer, it will be downloaded. Like the classid attribute, you can cut and paste this value into your HTML file.

Notice that code and codebase (the original codebase) are now parameters, instead of attributes of the <object> tag.

Now, here is how you would rewrite your HTML for Netscape Navigator:

<embed
       type="application/x-java-applet;version=1.3"
       pluginspace="http://java.sun.com/products/plugin/1.3/plugin-install.html"
       code="AppletThatRequiresPlugin.class"
       codebase="./classFiles/"
       width=50 height=50>
    <param name="animal" value="fish">
    <noembed>
         Support for JDK 1.3 could not be found.
    </noembed>
</embed>
You have probably noticed that it is much easier to convert from the <applet> tag to the <embed> tag. All that needs to be added are the type and pluginspace attributes. The pluginspace attribute (like the <object> tag codebase attribute for Internet Explorer) specifies the location of the Java Plug-in JRE for downloading. The <noembed> tag can be used for two purposes. First, if the <embed> tag fails to work, the <noembed> and </noembed> tags will be displayed as an error message. Secondly, if a browser cannot recognize the <embed> tag, the <noembed> tag can be used for supplementary content.

If you have not run off to get the HTML converter yet, you may be wondering if you are going to have to implement two pages (one for Internet Explorer and one for Netscape Navigator, neglecting the myriad of other browsers) for each applet. You don't need to and you don't even need to learn JavaScript to do it (although that works as well). We are going to exploit some neat facts about HTML and these two browsers. Let us look at the HTML first:

<object
       classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
       codebase="http://java.sun.com/products/plugin/1.1/jinstall-11-win32.cab#Version=1,3,0,0"
       width=50 height=50>
    <param name="code" value="AppletThatRequiresPlugin.class">
    <param name="codebase" value="./classFiles/">
    <param name="animal" value="fish">
    <comment>
	<embed
	       type="application/x-java-applet;version=1.3"
	       pluginspace="http://java.sun.com/products/plugin/1.3/plugin-install.html"
               code="AppletThatRequiresPlugin.class"
	       codebase="./classFiles/"
	       width=50 height=50>
	    <param name="animal" value="fish">
	    <noembed>
		Support for JDK 1.3 could not be found.
	    </noembed>
	</embed>
    </comment>
</object>
Let us think about what will happen when this page is displayed with Internet Explorer. As we saw earlier, Internet Explorer recognizes the <object> tag and displays the applet. The first neat fact that we use is that Internet Explorer ignores the <comment> and </comment> tags and everything between them. As a result, it will completely skip over the <embed> tag used for Netscape Navigator. Now we can be sure that our page will work with Internet Explorer. But what about Netscape Navigator? The second neat fact we use is that Netscape Navigator ignores the <object> tag with that ActiveX Control ID as the classid and the subsequent parameters. It also ignores the <comment> tag, going straight for the <embed> tag with which it is familiar. Your page now works with both browsers.

This concludes the first part of the applet notes. So far, you've learned how to write a bit of HTML, how to compile and run an applet, how to use some Graphics class methods, how the life cycle of an applet proceeds, how parameters work, how to use the <applet> and <object> tags, and how to rewrite your HTML to use the Java Plug-in. Once you have had some experience with GUI programming and Swing, you will learn more about the applet API and how to convert graphical applications to applets.