Vous êtes sur la page 1sur 11


In Java

What is an Applet?
Applets are small Java programs that are embedded in
Web pages. They can be transported over the Internet
from one computer (web server) to another (client
computers). They transform the web into rich media and
support the delivery of applications via the Internet. It is
also a special Java program that can be embedded in
HTML documents. A Java applet is a Java program
written in a special format to have a graphical user
interface. The graphical user interface is also called a
GUI , and it allows a user to interact with a program by
clicking the mouse, typing information into boxes, and

Applet and HTML file

Applet Security
Security is a big concern in the Java environment because any web page a user retrieves may cause
Java code to be transferred and executed. The user may not even be aware that a Java applet is
running. One security checkpoint occurs as the applet is being interpreted. As each of the required
classes is loaded, the JVM's Class Loader checks the code for attempts to violate security. If any are
found, the applet is not allowed to run.
Java applets are further prevented from tampering with a user's local machine by the "sandbox"
security model, which forms a barrier between the applet and the rest of the user's system. Being
contained within this barrier, the applet isn't allowed to do any of the following:
Read or write to the local file system
Modify or delete files from the local file system
Create or rename directories on the local file system
List directory contents or check for the existence of a file
Obtain file attribute information such as size, type, modification time
Create network connections to computers other than the source of the applet
Listen or connect via ports on the local system
Read from or modify various local system properties
Halt the execution of the Java interpreter
Load local libraries or invoke local executables

Uses of applets
Today, applets are used mainly to add two things to the
web environment: animation and interactivity. Java is
ideally suited to provide fairly sophisticated image
animation without having to constantly contact the
server. Java also enhances interactivity by providing a
way to validate user data entered into a form before it is
sent off to the server. These animation and interactivity
features could even be combined to provide easier
navigation through a complex user interface.

Applet life cycle

These methods are known as "callback methods" as they

are called automatically by the browser whenever required
for the smooth execution of the applet. Programmer just
write the methods with some code but never calls.

Applet Life cycle call back methods

init() is first method to call when the applet is loaded.
It is called exactly once in an applets life.

In this method, you initialize variables, read PARAM tags, register events, set
the foreground and background color of an applet.
init() is not compulsory to override it.
start() is the second method calls after the init() method.
In this method you can perform work like start thread execution.
start() is called each time an applets HTML document is displayed onscreen
but It is called at least once in an applets life cycle.
start() is not compulsory to override it.
paint() is used to draw output on applet window.
paint() is called each time your applets output must be redrawn .
This method takes a java.awt.Graphics object as parameter. This class includes
many methods of drawing necessary to draw on the applet window.
This is the place where the programmer can write his code of what he expects
from applet like animation etc. This is equivalent to run method of thread.

Applet Life cycle call back methods

stop() is complement of start() method.

called at least once in an applets life, when the browser leaves the page
in which the applet is embedded.

stop() is

In this method the applet becomes temporarily inactive.

An applet can come any number of times into this method in its life cycle and can go back to the active state
(paint() method) whenever would like.
It is

equivalent to the blocked state of the thread.

destroy() is last method to called during an applet life cycle.

called exactly once in an applets life, just before the browser unload
the applet.

destroy() is

This is the end of the life cycle of applet. It is the best place to have cleanup code. It is equivalent to the dead
state of the thread.

<applet> tag
HTML provides an <applet>...</applet> container tag for embedding Java
applet, with the following attributes:
code="JavaClassName": Specifies the applet class name, with the ".class" extension.
width, height: Specify the width and height of the applet's display area inside the
alt: (optional) alternate text if the applet fails to be displayed.
codebase: (optional) specifies the base path of the applet's class. The default base
path for the applet is the same path as the HTML file.
archive: (optional) specifies the JAR file that contains the applet classes. If your applet
involves more than one classes, it is common and more efficient to jar (i.e., zip) them
together into a single JAR file for distribution.