Recipe 18.1 Embedding Java in a Web Page


Problem

You need to deploy a Java applet.

Solution

Use an <applet> tag in an HTML page.

Discussion

While this is not the place for a dissertation on the details of HTML, you should at least know that HTML is a tag-based textual language for writing web pages. The tags (officially called elements) have short names, such as p for paragraph and a for anchor (hyperlink). Tag names can be written in uppercase or lowercase, with a preference for lowercase because the emerging standard XHTML[1] requires lowercase. Tags are surrounded by angle brackets, < and >. Modifiers, called attributes, go between the tag name and the close angle brackets. For example, the body of a web page might be introduced by <body bgcolor="white">, which gives that page the specified background color. Most tags, including body and p, have a corresponding end tag, consisting of a forward slash character (/) and the name of the tag. A paragraph, for example, should begin with <p> and end with </p>.

[1] XHTML is HTML written as though it were XML; see Chapter 21 for XML information.

In days of yore, it was common to simply use <P> between paragraphs, but this mistake stems from not understanding the nature of HTML tags as containers. It was also common to omit the quotation marks around attribute values. You still see old pages done this way and old books or web pages recommending this. You may even see a few examples of that in old code of mine!

The most common way to embed a Java applet is using an <applet> tag. Other tags for applets include <object> and <embed>, which I discuss briefly in Recipe 23.6. The <applet> tag has three required parameters (code, width, and height) and several optional ones. Table 18-1 lists these parameters.

Table 18-1. Applet parameters

Parameter

Description

code

Name of applet class to run

object

Name of serialized applet to run

width

Width in pixels for applet display

height

Height in pixels for applet display

codebase

Directory (URL) from which to load class file; needed only if different from place where the HTML page itself is loaded from

archive

List of JAR archives in which to look for applet and resources

alt

Alternate text to display if applet can't be loaded

name

Name of this applet instance

align

Horizontal alignment

vspace

Vertical space around applet, in pixels

hspace

Horizontal space around applet, in pixels


You may also wish to pass some parameters in to the applet. Since an applet has no main method, there is no command-line communication with the applet. Hence, the applet parameters are included in the HTML page: the <param> tags go between the <applet> and </applet> tags. The following HTML file demonstrates many of these parameters:

<applet      code="DemoApplet.class" width="400 " height="75"      codebase="http://www.darwinsys.com/applets/"      >      <param  name="text"  value="Java is fun!">      <hr / >      If you were using a Java-enabled browser,      you would see the graphical results instead of this paragraph.      <hr />  </applet>



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net