- Details
- Written by Nam Ha Minh
- Last Updated on 09 August 2019   |   Print Email
An applet is a Java program runs inside a web browser. Typically, you write the applet as a normal Java class (extending from JApplet class) and embed it into an HTML page using the
<applet> tag so the users can see and interact with your applet. This article describes various examples of how to display a Java applet inside an HTML page:
1. Syntax of <applet> tag
According to Oracle
documentation, following is a complete syntax of the
<applet> tag:
<APPLET
CODEBASE = codebaseURL
ARCHIVE = archiveList
CODE = appletFile...or... OBJECT = serializedApplet
ALT = alternateText
NAME = appletInstanceName
WIDTH = pixels HEIGHT = pixels
ALIGN = alignment
VSPACE = pixels HSPACE = pixels
>
<PARAM NAME = appletAttribute1 VALUE = value>
<PARAM NAME = appletAttribute2 VALUE = value>
. . .
alternateHTML
</APPLET>
The elements in bold and red are required. Others are optional. Put the
<applet> tag with its attributes inside the
<body> element of the HTML page, it can be nested in other container tags like
<div> or
<table>. All the applet’s files (classes, jars, resources, etc) should be placed in the same folder as the HTML page.
2. The simplest way to show a Java applet
Suppose you write an applet in
SimpleApplet.java file and compile it to
SimpleApplet.class file, the following code quickly shows off your applet:
<applet code="SimpleApplet.class"></applet>
Or set size for the applet explicitly:
<applet code="SimpleApplet.class" width=”125” height=”125”></applet>
3. Show a Java applet bundled in a jar file
It’s very common that you package the applet and its related classes as a single jar file. In this case, use the following code:
<applet
archive="SimpleApplet.jar"
code="net.codejava.applet.SimpleApplet.class">
</applet>
That displays the applet bundled in a jar file named
SimpleApplet.jar and the applet class file is
SimpleApplet resides under the package
net.codejava.applet.
4. Show a Java applet with parameters
Sometimes you need to send some parameters from the HTML page to the Java applet. In this case, use following code:
<applet
archive="SimpleApplet.jar"
code="net.codejava.applet.SimpleApplet.class">
<param name="user" value="tom">
<param name="password" value="secret">
</applet>
That sends two parameters called
user and
password with corresponding values to the applet. And inside the applet, we can access these parameters as follows:
String user = getParameter("user");
String password = getParameter("password");
5. Show a Java applet having external jar files
In case your applet comes with external library jar files, specify these jar files as follows:
<applet
archive="SimpleApplet.jar, mail.jar, video.jar"
code="net.codejava.applet.SimpleApplet.class">
</applet>
In the above code, we specify two additional jar files
mail.jar and
video.jar which are required by the applet. The jar files are separated by commas. If the jar files in a directory:
<applet
archive="SimpleApplet.jar, lib/mail.jar, lib/video.jar"
code="net.codejava.applet.SimpleApplet.class">
</applet>
In the above code, the additional jar files are placed under
lib directory.
6. Be prepared if the user cannot run your applet
In practice, the users may have problems of running your applet, such as the JRE/JDK or Java Plug-in have not installed, or their browser’s setting prevents Java applet from running, or even the browser does not understand the
<applet> tag. So you need to anticipate such cases, for example:
<applet
archive="SimpleApplet.jar"
code="net.codejava.applet.SimpleApplet.class"
alt="You need to have compatible browser to run this Java applet">
Your browser does not support running applet. <br>
Please install Java Plug-in from <a href="http://java.com">java.com</a>
</applet>
In the above code, we use the
altattribute and alternate HTML text between the opening and closing elements of the
<applet> tag to tell the browse should display that information if it cannot run the applet.
Other Java Applet Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on
Facebook and watch
his Java videos you YouTube.