The appletviewer


[Page 821 (continued)]

The appletviewer

The appletviewer tool lets you run Java applets without using a Web browser. This command takes the following form:


appletviewer [ threads flag] [ options]   url ...

The optional threads flag tells Java which of the various threading options to use. This is system dependent. For details on this feature and the command-line options, refer to Sun's Web site.


[Page 822]

The appletviewer will connect to one or more HTML documents specified by their Uniform Resource Locators (URLs). It will display each applet referenced in these documents in a separate window. Some example commands would be:

appletviewer http://www.domain.edu/~account/myapplet.html appletviewer myapplet.html 


In the first case, the document's full path name is given. In the second case, since no host computer is mentioned, appletviewer will assume that the applet is located on the local host and will search the class path for myapplet.html.

AppletViewer tags


Once appletviewer retrieves the HTML document, it will find the applet by looking for either the object, embed, or applet tag within the document. The appletviewer ignores all other HTML tags. It just runs the applet. If it cannot find one of these tags, the appletviewer will do nothing. If it does locate an applet, it starts a runtime environment, loads the applet, and then runs the applet's init() method. The applet's init() must have the following method signature:

public void init() 


The applet Tag

The applet tag is the original HTML 3.2 tag used for embedding applets within an HTML document. If this tag is used, the applet will be run by the browser, using the browser's own implementation of the Java Runtime Environment (JRE).

Note, however, that if your applet uses the latest Java language features and the browser is not using the latest version of JRE, the applet may not run correctly. For example, this might happen if your applet makes use of Swing features that are not yet supported in the browser's implementation of the JRE. In that case, your applet won't run under that browser.

To ensure that the applet runs with the latest version of the JREthe one provided by Sun Microsystemsyou can also use the object or embed tags. These tags are used to load the appropriate version of the JRE into the browser as a plugin module. A plugin is a helper program that extends the browser's functionality.

The applet tag takes the following form:

<applet    code="yourAppletClass.class"    object="serializedObjectOrJavaBean"    codebase="classFileDirectory"    width="pixelWidth"    height="pixelHeight" >    <param name="..." value="...">     ...    alternate-text </applet> 



[Page 823]

You would use only the code or object attribute, not both. For the programs in this book, you should always use the code tag. The code tag specifies where the program will begin executionthat is, in the applet class.

The optional codebase attribute is used to specify a relative path to the applet. It may be omitted if the applet's class file is in the same directory as the HTML document.

The width and height attributes specify the initial dimensions of the applet's window. The values specified in the applet tag can be overridden in the applet itself by using the setSize() method, which the applet inherits from the java.awt.Component class.

The param tags are used to specify arguments that can be retrieved when the applet starts running (usually in the applet's init() method). The methods for retrieving parameters are defined in the java.applet.Applet class.

Finally, the alternative-text portion of the applet tag provides text that would be displayed on the Web page if the appletviewer or browser is unable to locate the applet.

Here is a simple example of an applet tag:

<applet     code="HelloWorldApplet.class"     codebase="classfiles"     width="200"     height="200" >     <param name="author" value="Java Java Java">     <param name="date" value="May 1999">     Sorry, your browser does not seem to be able to     locate the HelloWorldApplet. </applet> 


In this case, the applet's code is stored in a file name HelloWorldApplet.class, which is stored in the classfiles subdirectorythat is, a subdirectory of the directory containing the HTML file. The applet's window will be 200 x 200 pixels. And the applet is passed the name of the program's author and the date it was written. Finally, if the applet cannot be located, the "Sorry . . . " message will be displayed instead.

The object Tag

The object tag is the HTML 4.0 tag for embedding applets and multimedia objects in an HTML document. It is also an Internet Explorer (IE) 4.x extension to HTML. It allows IE to run a Java applet using the latest JRE plugin from Sun. The object tag takes the following form:

<object     class     codebase="url for the plugin program"     width="pixelWidth"     height="pixelHeight" >     <param name="code" value="yourClass.class">     <param name="codebase" value="classFileDirectory">      ...     alternate-text </object> 



[Page 824]

Note that parameters are used to specify your applet's code and codebase. In effect, these are parameters to the plugin module. An example tag that corresponds to the applet tag for the HelloWorldApplet might be as follows:

<object     class     codebase="http://java.sun.com/products/plugin/"     width="200"     height="200" >     <param name="code" value="HelloWorldApplet.class">     <param name="codebase" value="classfiles">     <param name="author" value="Java Java Java">     <param name="date" value="May 1999">     Sorry, your browser does not seem to be able to     locate the HelloWorldApplet. </object> 


If the browser has an older version of the plugin than shown in the codebase attribute, the user will be prompted to download the newer version. If the browser has the same or a newer version, that version will run. In theory Netscape 6 should also work the same as IE. For further details on how to use the object tag, see Sun's plugin site at:

http://java.sun.com/products/plugin/

The embed Tag

The embed tag is Netscape's version of the applet and object tags. It is included as an extension to HTML 3.2. It can be used to allow a Netscape 4.x browser to run a Java applet using the latest Java plugin from Sun. It takes the following form:

<embed     type="Type of program"     code="yourAppletClass.class"     codebase="classFileDirectory"     pluginspage="location of plugin file on the web"     width="pixelWidth"     height="pixelHeight" >     <noembed>     Alternative text     </noembed> </embed> 


The type and pluginspage attributes are not used by the appletviewer, but they are necessary for browsers. They would just be ignored by the appletviewer.


[Page 825]

For example, an embed tag for HelloWorldApplet would be as follows:

<EMBED     type="application/x-java-applet;version=1.4"     width="200"     height="200"     code="HelloWorldApplet.class"     codebase="classfiles"     pluginspage="http://java.sun.com/products/plugin/" >    <NOEMBED>        Sorry. This page won't be able to run this applet.    </NOEMBED> </EMBED> 


It may be possible to combine the applet, embed, and object tags in the same HTML file. Sun provides much more information, as well as demo programs, on its plugin Web site:

http://java.sun.com/products/plugin/




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

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