In this article we will look at a sample chapter from Wrox's HTML 4.01 Programmer's Reference book. This chapter focuses on the HTML object tag and shows a variety of advanced ways to use and manipulate it.
Sample Chapter: HTML 4.01 Programmers Reference - Inserting a Java Applet with the Object Element (Page 8 of 10 )
With the <applet> element's deprecation in the HTML 4.01 standard, the way to embed Java applets is with the <object> element. It isn't quite as straightforward as all that. Java has a somewhat checkered history with web browsers. With its creation by Sun in the mid-1990s, the main browser vendors of the time (Netscape and Microsoft) were quick to realize its potential. They both quickly included versions of the Java Run-Time Environment within their browsers, which could be made use of by the <applet> element. However, Sun updated the Java environment on a regular basis, and so the browsers were left supporting out-of-date versions of the software.
While the creation of Opera didn't make much impact on the two main browsers, it did make the browser agent a much smaller entity, and was the first to consider making the Java Virtual Machine an optional add-on to the main browser. This has some ramifications now for putting Java applets in our web pages, because we still can't assume that every browser will automatically have Java installed (and even if we could some people would disable it anyway). Also, even if they did have it installed, it might not be the most up-to-date version, or a version recent enough to run the applet. It also means if we want to run Java applets on a web browser without native Java support, we must install the Java plug-in first via the codebase attribute. However, as downloading any component via the codebase attribute is a lengthy process, we suggest that if you really intend using Java (even if you're just browsing Java applets) then you should use a browser that contains the Java Virtual Machine.
Ok, we're going to borrow a very simple example Java Clock applet from the Sun Microsystems site, for which Sun holds the copyright, but the license allows it to be freely distributed. This is located here.
This should be saved as Clock2.java on your own machine, after which you need to use the JDK 1.3 to compile it as Clock2.class.
You can compile it using the command prompt/UNIX command line using the following code:
> javac Clock2.class
We're not interested in the internal workings of this code, so we're not going to explain how it works. It is enough to know that it will display a vector graphic clock on our web page. What we are interested in doing is embedding this applet in a web page, within the <object> element, so that it works in as many browsers as possible.
With Internet Explorer it's enough to supply the details in the classid attribute:
<object codetype="application/java" classid="java:Clock2.class" width="400" height="200"> Your browser is ignoring the "object" element.</object>
However it is possible to specify the latest version of the JDK as follows:
<object codetype="application/java;version=1.3" classid="java:Clock2.class" width="400" height="200"> Your browser is ignoring the "object" element.</object>
In which case we are dependent on having a version of IE with a recent enough JDK. In both cases it should result in something along the lines of:
However, for the browsers that don't include native Java support this won't be enough, and we will also need to indicate the location of a Java plug-in to be able to run Java applets. We need to specify the codebase as follows – this will be ignored if the browser already has native Java support. In IE versions without the Java SDK the code would be as follows:
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="300" height="200" codebase="http://java.sun.com/products/ plugin/1.3/jinstall-13-win32.cab#Version=1,2,0,0"> <param name="code" value="Clock2.class"> <param name="type" value="application/x-java-applet;version=1.3"> Your browser is ignoring the "object" element.</object>
However, the .cab executable specified is something unique to IE, and to get it work in Netscape 6 we actually need to specify the Sun Java plug-in instead:
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="300" height="200" codebase="http://java.sun.com/products/plugin/ 1.3/plugin-install.html"> <param name="code" value="Clock2.class"> <param name="type" value="application/x-java-applet;version=1.3"> Your browser is ignoring the "object" element.</object>
Here we've used a version of Netscape 6, without the Java SDK included, and the output will look like this:
Of course this still leaves the problem of what to do with browsers that don't support the <object> element entirely, such as Netscape 4. In such cases, we can tuck the <applet> tag into the code as follows, making sure to comment it out (for Internet Explorer):
The IE-specific <comment> tag is the only one available, and if it isn't used, then both the <object> and the <applet> elements are interpreted by IE, and the applet will be displayed twice. This is direct contradiction to what we said earlier about objects; the expected behavior should be to ignore the <applet> element. However, with the <applet> element in IE, there seems to be this anomaly, which requires the use of the <comment> tag. There is more on the <applet> element in the Chapter 15.