Chapter 14 Applets and HTML
· <APPLET> tag specifies applet
information in a HTML page
· It must be enclosed between
<BODY> and </BODY>
· </APPLET> tag is mandatory to
close the <APPLET> tag
· CODE, WIDTH and HEIGHT are
mandatory attributes within <APPLET> tag. Their order is not significant.
Instead of the class file, applet can specified via a serialized file using
OBJECT attribute. If we specify OBJECT attribute CODE attribute is not specified
and vice versa.
· In HTML 4.0, OBJECT tag can
be specified instead of APPLET tag. With OBJECT tag, we specify the applet with
CLASSID attribute.
· The following are other
optional tags
|
Tag Name |
Purpose |
|
CODEBASE |
Directory
for the applet’s class |
|
ALT |
Alternate
text for browsers with no support for applets but can understand <APPLET>
tag |
|
HSPACE |
Left/Right
boundaries b/w other HTML elements on a page |
|
VSPACE |
Top/Bottom
boundaries b/w other HTML elements on a page |
|
ALIGN |
Alignment
with respect to other HTML elements |
|
NAME |
Name of the
applet for inter-applet communication |
|
ARCHIVE |
Name of the
JAR file (Lot of files can be downloaded in a single download to reduce
the time needed) Even multiple jar files can be specified, separated by
commas. |
|
OBJECT |
Specified if
CODE attribute is not present and vice versa. Applet is read in from the
specified serialized file. |
· Between <APPLET> and
</APPLET>, PARAM tags can be specified. These are used to pass parameters from
HTML page to the applet.
· <PARAM NAME = ”name” VALUE =
”value”>
· Applets call
getParameter(name) to get the parameter. The name is not case sensitive here.
· The value returned by
getParameter is case sensitive, it is returned as defined in the HTML page.
· If not defined, getParameter
returns null.
· Text specified between
<APPLET> and </APPLET> is displayed by completely applet ignorant browsers, who
cannot understand even the <APPLET> tag.
· If the applet class has only
non-default constructors, applet viewer throws runtime errors while loading the
applet since the default constructor is not provided by the JVM. But IE doesn’t
have this problem. But with applets always do the initialization in the init
method. That’s the normal practice.
· Methods involved in applet’s
lifecycle.
|
Method |
Description |
|
void init() |
This method
is called only once by the applet context to inform the applet that it has
been loaded into the system. Always followed by calls to start() and
paint() methods. Same purpose as a constructor. Use this method to perform
any initialization. |
|
void start() |
Applet
context calls this method for the first time after calling init(), and
thereafter every time the applet page is made visible. |
|
void stop() |
Applet
context calls this method when it wants the applet to stop the execution.
This method is called when the applet page is no longer visible. |
|
void
destroy() |
This method
is called to inform the applet that it should relinquish any system
resources that it had allocated. Stop() method is called prior to this
method. |
|
void
paint(Graphics g) |
Applets
normally put all the rendering operations in this method. |
· Limitations for Applets:
· Reading, writing or deleting
files on local host is not allowed.
· Running other applications
from within the applet is prohibited.
· Calling System.exit() to
terminate the applet is not allowed.
· Accessing user, file and
system information, other than locale-specific information like Java version, OS
name and version, text-encoding standard, file-path and line separators, is
prohibited.
· Connecting to hosts other
than the one from which the applet was loaded is not permitted.
· Top-level windows that an
applet creates have a warning message for applets loaded over the net.
· Some other methods of Applet
class
|
Method |
Description |
|
URL
getDocumentBase() |
Returns the
document URL, i.e. the URL of the HTML file in which the applet is
embedded. |
|
URL
getCodeBase() |
Returns the
base URL, i.e. the URL of the applet class file that contains the applet. |
|
void
showStatus(String msg) |
Applet can
request the applet context to display messages in its “status window”. |
