Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

When to use applets instead of HTML forms: Three easy techniques

Using applets for web page menus and database interactions

  • Print
  • Feedback

Page 2 of 7

Deploying Java applets with runApplet

Applet display has been a part of the web since the mid 1990s, and is a classic example of the chaos caused by differing browser implementations of the HTML specification. The original Applet tag was deprecated in HTML 4.01, which specified using the Object tag instead. The specification was a bit vague on how the Object tag should be implemented, however, and so support is inconsistent. Internet Explorer works well with the Object tag, but Firefox and Chrome prefer the Embed tag. In response, Oracle offers a script that makes deploying an applet in a mixed-browser environment feasible. The script is based on a single function called runApplet, which accepts three parameters:

  • attributes represents the names and values of the attributes used in the Applet, Object, and Embed tags.
  • parameters represents the names and values of the parameters to be passed to the applet.
  • version represents the minimum version of the JRE software that is required to run the given applet.

In order to use runApplet you need to include the following script and then create JavaScript code that sets the values of the three parameters and invokes the function, as shown in Listing 1.

Listing 1. Using deployJava script

<script src="http://www.java.com/js/deployJava.js"></script>

<script>
  var attributes = {id: "AppletID",
              code: "Applet.class",
              archive: "Applet.jar",
              width:"100%", height:"40" };
  var parameters = {param1:"value1", param2:"value2", param3:"value3",
                     , paramn:"valueN"};
  var version = '1.6';
  deployJava.runApplet(attributes, parameters, version);
</script>

You could write your own script to sort out which browser is running and which tag to use, but I prefer letting Oracle manage this headache. Including runApplet gives me some assurance that my applets will work and relieves me of the responsibility of continually checking browser-version and tag combinations.

Note that Oracle’s deployJava script also enables programmers to invoke applets using JNLP and request additional permissions. We don't need either of those features for the techniques described in this article.

  • Print
  • Feedback

Resources