Chapter Two
Java Applet
Java Applet
• Applet is a special type of program that is embedded in the webpage to generate
the dynamic content.
• It runs inside the browser and works at client side.
Advantage of Applet
• There are many advantages of applet. They are as follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many plateforms, including Linux, Windows,
Mac Os etc.
Drawback of Applet
• Plugin is required at client browser to execute applet
Hierarchy of Applet
As displayed in the above diagram, Applet class extends Panel. Panel class
extends Container which is the subclass of Component.
Lifecycle of Java Applet
1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Lifecycle methods for Applet:
The java.applet.Applet class have 4 life cycle methods and
java.awt.Component class provides 1 life cycle methods for an applet
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life
cycle methods of applet
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized. It
is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.
• public void paint(Graphics g): is used to paint the Applet. It provides Graphics
class object that can be used for drawing oval, rectangle, arc etc.
How to run an Applet?
their are two ways to run an applet
• By html file.
• By appletViewer tool (for testing purpose).
The Different between Java Application and Java Applet
Java application and Java applet both are Java programs, but there is a slight
difference between Them. like:-
• Java applications are types of stand-alone applications that run directly on the
underlying operating system with the help of virtual machine.
• The execution of the Java application always begins with the main( ) method,
while in the case of an applet, initialization takes through the init( ).
• These perform various general operations for their users and do not require any
APIs or browsers enabled by Java.
• On the other hand, the applets are small programs that can be embedded into a
web page.
• The applets are used to make the website more dynamic and entertaining.
Java Application
• Java application is basically a Java program (collection of instructions) that runs stand
alone in a client or server.
Features
• It resembles Java programs strongly.
• We do not require any web browser for the execution of these programs. Execution can be
easily done via the local system.
• The implementation of these applications requires main( ) function.
• The local file systems and networks are fully accessed by these applications.
Java Applet
• The Java applet works on the client side, and runs on the web browser.
• It is a Java application that the user can easily embed on a web page
Features
• Java applet is a small and easy-to-write Java program.
• One can easily install Java applet along with various HTML documents.
• One needs a web browser (Java based) to use applets.
• Applet do not have access to the network or local disk and can only access browser-specific services.
• It cannot perform system operations on local machines.
• Java applet cannot establish access to local system.
Java Application Vs. Java Applet
Parameters Java Application Java Applet
Meaning A Java Application also known as The Java applet works on the client
application program is a type of side, and runs on the browser and
program that independently makes use of another application
executes on the computer. program so that we can execute it.
Requirement of main( ) method Its execution starts with the main( ) It does not require the use of any
method only. The use of the main( ) main() method. Java applet
is mandatory. initializes through init( ) method.
Execution It cannot run independently, but It cannot start independently but
requires JRE to run. requires APIs for use (Example. APIs
like Web API).
Installation We need to install the Java Java applet does not need to be
application first and obviously on pre-installed.
the local computer.
Connectivity with server It is possible to establish It cannot establish connection to
connections with other servers. other servers.
Operation It performs read and write tasks on It cannot run the applications on
a variety of files located on a local any local computer.
computer.
File access It can easily access a file or data It cannot access the file or data
available on a computer system or found on any local system or
device. computer.
Security Java applications are pretty Java applets are less reliable. So,
trusted, and thus, come with no they need to be safe
security concerns.