Running applets inside a Web browser
To run this program you must place it inside a Web page and view that page inside your Java-enabled Web browser. To place an applet inside a Web page, you put a special tag inside the HTML source for that Web page to tell the page how to load and run the applet.
This process used to be very simple, when Java itself was simple and everyone was on the same bandwagon and incorporated the same Java support inside their Web browsers. Then you might have been able to get away with a very simple bit of HTML inside your Web page, like this:
<applet code=Applet1 width=100 height=50>
Then along came the browser and language wars, and we (programmers and end users alike) lost. After awhile, Sun realized that we could no longer expect browsers to support the correct flavor of Java, and the only solution was to provide some kind of add-on that would conform to a browsers extension mechanism. By using the extension mechanism (which a browser vendor cannot disablein an attempt to gain competitive advantagewithout breaking all the third-party extensions), Sun guarantees that Java cannot be shut out of the Web browser by an antagonistic vendor.
With Internet Explorer, the extension mechanism is the ActiveX control, and with Netscape, it is the plug-in. In your HTML code, you must provide tags to support both, but you can automatically generate the necessary tags with the HTMLconverter tool that comes with the JDK download. Heres what the simplest resulting HTML page looks like for Applet1 after running HTMLconverter on the preceding applet tag:
<!-- HTML CONVERTER -->
classid = "clsid:CAFEEFAC-0014-0001-0000-ABCDEFFEDCBA"
codebase = "http://java.sun.com/products/plugin/autodl/jinstall-1_4_1-windows-i586.cab#Version=1,4,1,0"
WIDTH = 100 HEIGHT = 50 >
<PARAM NAME = CODE VALUE = Applet1 >
<PARAM NAME = "type" VALUE = "application/x-java-applet;jpi-version=1.4.1">
<PARAM NAME = "scriptable" VALUE = "false">
type = "application/x-java-applet;jpi-version=1.4.1"
CODE = Applet1
WIDTH = 100
HEIGHT = 50
scriptable = false
pluginspage = "http://java.sun.com/products/plugin/index.html#download">
<APPLET CODE = Applet1 WIDTH = 100 HEIGHT = 50>
Some of these lines were too long and had to be wrapped to fit on the page. The code in this books source code (downloadable from www.BruceEckel.com) will work without having to worry about correcting line wraps.
The code value gives the name of the .class file where the applet resides. The width and height specify the initial size of the applet (in pixels, as before). There are other items you can place within the applet tag: a place to find other .class files on the Internet (codebase), alignment information (align), a special identifier that makes it possible for applets to communicate with each other (name), and applet parameters to provide information that the applet can retrieve. Parameters are in the form:
<param name="identifier" value = "information">
and there can be as many as you want.
The source code package for this book (freely downloadable at www.BruceEckel.com) provides an HTML page for each of the applets in this book, and thus many examples of the applet tag, all driven from the index.html file corresponding to this chapters source code. You can find a full and current description of the details of placing applets in Web pages at java.sun.com.