about


Scribe:

chrono

blog

Time

science!

book musings

gov

'punk

missle defense

jams

antiques

cultcha blog


Construct:

scripts & programs

on the town

snaps


Self:

creds (PDF)

key

missive


Embedding Java Applets in a Web page

For most Java programming classes, the programs that students write are submitted as stand-alone executables. But how does one put them on the Web and offer them as a service for others to use?

You can make an applet. An applet is a Java program that can be run from within a Web browser (on a system that runs the Java Runtime Engine [JRE] that is, which, as of this writing, the iPhone doesn't).

Pretty much anything you can do with the Java language you can do within an Applet. Both use the Java Virtual Machine (JVM). Though setting up the program to run as an Applet is a wee bit different than setting up as a stand-alone program.

Once, embedding an Applet on a Web page was easy. You'd simply use the <Applet> tag. That W3C has deprecated that tag, which means you shouldn't use it any longer (Though it still works in most all browsers).

Instead, you have to use either the <embed> tag for Mozilla-based browsers, and the <object> tag for Internet Explorer browsers. And, realistically, if you're planning to have any visitors at all, you should use both.

(Before we begin, you should test to see if you have Java properly installed on your machine, go here.)


First, here is the bare-bones markup you'd use to embed your applet using the <object> tag, for IE browsers. You can put this tag within the body of the html document:
<OBJECT 
  classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
  <PARAM name="code" value="[NAME OF THE YOUR PROGRAM].class">
</OBJECT>
The markup above was derived from this Java documentation page.

The classid tells the browser what program (In this case, the JRE) on the user's computer should run the code being pointed to (in this case the class file). The above series of numbers tells the browser to use the latest version of the JRE, though you can also specify some specific version (see documentation). You can also use optional attributes for setting height, width or other aspects of the applet. See a list of attributes here.


For the Mozilla family of browsers, you must use the embed tag. Embed is NOT supported by Internet Explorer and it is NOT recognized by the World Wide Web Consortium (W3C), but since Firefox et al do not support the Object tag, at least for Applets, you should use the embed tag. Here is the code for the Web page, again which can appear in the body:
      <embed code="[NAME OF YOUR PROGRAM].class"
        type="application/x-java-applet;version=1.6">
      </embed>
Again there are additional options you can add in.

It is worth noting for the beginner that with both the embed tag and the object tag, supplying the correct plug-in name is crucial. In this instance, for instance, "version=1.6.0" would NOT work. If your Web page is not running the Applet by now, it may be due to you not using the correct name. Search the Web for answers!


If you put your Java Applet out on the Web, however, you have to prepare yourself for both types of browsers. This site helpfully demonstrates how to use both with an html document:
<object 
  classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
  <PARAM name="code" value="[NAME OF YOUR PROGRAM].class">
    <comment>
      <embed code="[NAME OF YOUR PROGRAM].class"
        type="application/x-java-applet;version=1.6">
      </embed>
    </comment>
  



You can also post your program the old fashioned (i.e. deprecated) way, through the applet tag. Most browsers still recognize the applet tag, even though is not even supported in html 4.1 or even xhtml. Here's how:
<body>
<applet code = "[NAME OF YOUR PROGRAM].class">
</applet>
Please to note that all the description about the applet is actually in the opening tag. So you have to put a close bracket onto the end, and then you have to do a close applet ("</applet>") tag as well.

Within the Applet tag, you can also specify attributes like width ("width = [fill in value here]"), height ("height ="), vertical margin ("vspace =") horizontal margin ("hspace ="), alignment ("align =") and others. Go here for a full list of optional attributes.


What about the code itself? You would build an Applet pretty much the same way you would build a stand-alone Java program.

Just like on the command line, what gets executed is a class file (a file with a .class extension), one that has been compiled from Java source code.

In the program code itself, there doesn't have to be a "main" method, for instance. Instead, this subclass is used:

import javax.swing.*;
public class TestApplet2 extends JApplet {
public void init() {
  add (new JLabel("Hello World", JLabel.CENTER));
    }
    }
After this, control of the applet is handed by "init" "start" "stop" and "destroy" control the applet.

For the user-interface, you can use some libraries, like Swing, but more on that later. If you don't wish to test your code through a browser each time out, you can also use the Java Applet Viewer, which is in the SDK, and can be evoked from the command line.


All the code used in this entry, including Web pages for object, embed, applet and combined tagging, as well as a sample Java program, both before and after compilation, can be found in this zipped package. --Joab Jackson Taken from the book, "Introduction to Java Programming" (7th edition), by Y. Daniel Liang....
...as well as from the materials of a class I am taking.--Joab Jackson