Chapter 3: Using Java on Your Pages

index | next  
 
 
 
Listing 3.1 It's really pretty straightforward to add a Java applet to your Web page.

code listing
<HTML>
<HEAD>
  <TITLE>Hey, it's Java<TITLE>
</HEAD>
<BODY BGCOLOR=WHITE>
   <APPLET CODEBASE="classes"
-> CODE="HelloWorld.class" WIDTH=200 HEIGHT=50>
  </APPLET>

</BODY>
</HTML>


Figure 3.1
 
Fig. 1  This is what the HelloWorld applet looks like on a Web page.
 
 

Placing Java applets on your pages

Surprisingly, you can make extensive use of Java applets on your Web pages without ever learning the Java language. Listing 3.1 is an example of an HTML file that uses the APPLET tag to add an applet to the Web page.

To place a Java applet on your Web page:

1. < APPLET CODEBASE="classes" CODE='"HelloWorld.class" WIDTH=200 HEIGHT=50>

The APPLET tag is a standard part of HTML. The attributes we're using here are CODEBASE (the directory where the applet resides on the server), CODE (the name of the applet's class file), and HEIGHT and WIDTH (which tell the browser how much room the Java applet will take up on the page).

In this case, we're calling the HelloWorld applet. The class file (described in chapter 2) is named HelloWorld.class , and it resides in the classes directory. The width is 200 and the height is 50. Figure 3.1 shows what the applet looks like on a Web page.

2. </APPLET>

This ends the APPLET tag.

red check Tip

If you have applets that are shared by multiple pages on your site, use the CODEBASE attribute of the APPLET tag to refer to the applet in a common directory. That way, if you change the applet later, you won't have to touch the HTML files.