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 (as I do in this book) or lowercase, but be consistent. 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>. 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. You still see old pages done this way and, occasionally, very old books or web pages recommending this.

The most common method of embedding a Java applet is to use an APPLET tag. Other tags include OBJECT and EMBED, which I’ll discuss briefly in Section 23.6. The APPLET tag has three required parameters (CODE/OBJECT, WIDTH, and HEIGHT) and several optional ones. Table 17-1 lists these parameters.

Table 17-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

ARCHIVE

List of JAR archives in which to hunt 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 applet, for example, is an HTML file that 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>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.126