|
Applets are "little" applications in Java that are
designed to run from inside a browser. That is, an
applet can be executed using HTML commands.
Applets are in the java.applet
package.
Important Note: Java 2 is not
fully supported in either Netscape or Internet Explorer. At
the moment, Java 2 support is through a plug-in and is not inherent
in the browser. Java 2 is not supported on Macs at
all.
Applet Security Restrictions
Applets are client-side programs that are
executing on the user's (client's) machine, not the server that is
supplying the actual code. Because of this and the fact
that running an applet involves transmitting information back and
forth across the Internet, applets raise the possibility of unsuspecting
users running malicious programs that could read or modify information on
the user's computer. To combat this, Sun put security
restrictions on applets that are not present on applications.
These security restrictions primarily prevent applets from reading or
writing data from/to the client's computer. In addition,
an applet is forbidden from reading or writing data to any computer other
than the server that sourced it. People often refer to the
security walls around an applet as the "sandbox" in which it can
"play".
It is possible to create digitally "signed" applets that can
bypass the security restrictions, but that is beyond the scope of this
discussion.
Other Differences Between Applets and Applications
- No main() method: Applets do not need a main() method to exist
anywhere because they are running inside another program (the browser)
and are thus not a stand-alone program.
- No parameterized constructor: The browser calls the
default, unparameterized constructor of an Applet, so parameterized
constructors are not generally useful. Applets can be called
from the web page with parameters however. See the discussion of
the getParameters() method below.
- The init() method: An applet runs this
special method after the constructor is run. This
method is used instead of the constructor to initialize the applet.
- Applets are stay resident: Applets
stay in the computer's memory until the browser itself
closes. That is, even if the browser changes to a new web
page, the applet stays around. The applet's init()
method is only run once, no matter how many times the browser revisits
the page with the applet.
- The start() method: This method of an
applet is called whenever the applet is shown in the
browser. It is called after init() and every time the
page containing the applet is revisited. This method is useful for reinitializing the
applet.
- The stop() method: This method of an applet
is called whenever the browser leaves the web page containing the
applet. It is useful for performing any clean-up
needed by the applet to prepare it for a "dormant" state when the
browser is elsewhere. In particular, it is important to
shutdown any extra threads that are running and to free up other
resources.
- The destroy() method: This method of an Applet is called when the
browser itself exits to free up the applet's resources. It
is not usually called by the applet itself.
- The getParameter() method: This method of an Applet can be called from
within the applet to obtain the parameters, in String form, that were
passed to the applet by the web browser. The name of the
desired parameter is the input value of this method, and a string value
is returned.
- The getAppletInfo() method: This method of an Applet is
called by the browser, say from a Javascript script, to obtain
information about the applet.
- The getParameterInfo() method: This
method of an Applet is called by the browser, say from a Javascript
script, to obtain information about the parameters needed by an
applet.
HTML Code to Execute an Applet
<APPLET archive="[jar file name]" code="[name of applet].class"
width = [ width value] height = [ height value] > <PARAM name=
"[parameter #1 name]" value="[parameter #1 value]"> <PARAM name=
"[parameter #2 name]" value="[parameter #2
value]"> <PARAM name= "[parameter #3 name]" value="[parameter
#3 value]"> . . . </APPLET>
Description of tags and attributes
- <APPLET> tag: Declares what applet is to be run and its size on
the screen.
- jar file name: The archive atribute of the Applet tag is
optional. When used, it tells the browser what "jar" file
to use. A "jar" file is a Java ARchive file, which is
basically a zipped file with a .jar extension used to hold many
classes in a single file. Jar files are a very handy way
to manage all the classes needed for an applet or application.
- name of applet: The name of the Applet class to run.
Note that the code attribute requires that the .class extension
be specified.
- width and height values:
The width and height of the applet, in pixels, as displayed
inside the browser window.
- <PARAM> tags: The param tag is optional.
Each tag specifies one parameter. By using something like
Javascript, one can dynamically set the values of parameters.
- parameter #n name: The name by which the parameter can be
referenced. This is the name that the
Applet.getParameter() method uses to obtain the parameter value.
- parameter #n value: The string value of the
parameter. It is the responsibility of the applet itself to
convert the string to whatever type it needs.
Appletviewer
To run an applet from the command line, Sun's JDK provides a program
called Appletviewer. This program will run an HTML page
containing the <applet> tag. All other tags are
ignored. The applet comes up in a small window with just the
applet inside.
Syntax: appletviewer [HTML file with extension]
|