What Is Applet In Java?
A Java application that is integrated into a webpage is called an applet. It
functions as a front-end and is run within the web computer. It makes a
page more interactive and dynamic by operating inside the web browser.
Applets are hosted on web servers and inserted into HTML pages via the
OBJECT or APPLET tags.
It can be compared to a tiny application that runs on the address bar. In
addition to updating content in real-time and responding to human input,
it may also play basic puzzles or graphics.
The Life Cycle Of An Applet
The process by which an object is developed, launched, halted, and
demolished throughout an application's implementation is known as the
applet life cycle in Java. In essence, it has five main methods: paint(),
destroy(), stop(), init(), and start().The browser calls these methods in
order to perform their
actions.
1. Init()- The init() function is the first to be executed and is responsible for
initializing the applet. It can only be called once throughout the startup
process. The initialized objects are created by the web browser, which
executes the init() function located inside the applet after confirming the
authentication setting.
2. Start()- This function launches the applet and includes the applet's real
code. It begins directly following the call to the init() function. The start()
function is called each time the browser loads or refreshes. Additionally, it
is triggered when the applet is relaunched, improved, or switched across
tabs in the web interface. Until the init() function is used, it is in an idle
state.
3. Stop()- The applet's execution is terminated by using the stop()
function. Every time the applet is minimized, paused, or switched between
tabs on the web page, the stop() function is triggered. Again, the start()
function is called when we return to that page.
4. Destroy()- Once the applet completes its task, the destroy() function
terminates it. When the webpage-containing tab closes, or the applet
window closes, it is triggered. It is performed just once and expunges the
applet data from RAM. We can't restart the applet after it's been deleted.
5. Paint()- The Java Graphics class contains the paint() function. It is
employed in the applet to draw forms like squares, circles, trapeziums,
and so on. It runs following the call to the start() function and whenever
the web page or applet display resizes.
Types of Applets
Java applets can be classified as either local or remote, depending on
where they are stored and how easily they can be accessed.
1. Local Applet
We will write the Local Applet ourselves and then integrate it into
websites. A local applet is created locally and kept on the local machine.
When a web page detects a local applet in the Java system's memory, it
does not need to obtain data directly from the internet in order to
function. It is defined or provided by the pathname or folder name. When
constructing an applet, two properties are used: the source folder, which
defines the path name, and the code itself, which defines the filename
containing the applet's programming.
2. Remote Applet
The remote applet is stored or accessible on another computer that is
linked to the world over the internet. We must have internet access on the
system to be able to obtain and use the applet that resides on the other
machine. We need to be familiar with a remote applet's Uniform Resource
Locator (URL) or web location in order to find and download it.
Simple Applet Program In Java
"Hello, World" is a well-known illustration of a basic applet program in
Java. The code is as follows:
Java
import java. applet.*;
import java. awt.*;
public class HelloWorld extends
Applet
public
void
init()
setBackground(Color.white);
}
public
void
paint(Graphics g)
g.setColor(Color.black);
g.drawString("Hello, world!", 50, 50);
Advantages Of Java Applets
1. Interaction With Browsers
Through a smooth integration with online sites, applets let viewers engage
with dynamic information without exiting the browser. This results in
improved customer service while eliminating the need to obtain and set
up separate programs.
2. Diminished Server Traffic
Local execution of the applet's code allows functionality to occur on the
front end or the user's workstation. In doing so, the server's workload is
lessened, and it can process more inquiries and users at once.
3. Availability
Applets may adjust to changing frequency bands and computing power.
They can be compact and quick to load for users with poorer interactions
while providing more capability for those with greater capabilities.
Disadvantages Of Java Applets
1. Utilizing Excessive Performance And Resources
A common complaint about Java applets is their resource use, particularly
with complicated ones. In certain situations, they can even cause crashes,
hinder browser performance, and deplete the gadget's batteries. This
slowness and waste of resources are not acceptable in today's
performance-driven online environment.
2. Insufficient Development Support
Application interest declined when major browsers discontinued
supporting applets. Suppose you compare applet programming to
contemporary website creation tools. In that case, it becomes less
appealing and more difficult due to the decreased availability of
programming frameworks, libraries, and debugging resources.
3. Security Flaws
Because of their architecture, applications operate inside the user's
browser and provide access to a greater variety of resources than web
pages. But this authority has a cost: possible security lapses. Fraudulent
applets might exploit vulnerabilities to get access to private information,
download malware, or interfere with system functions.
How To Run An Applet Through An HTML File?
Despite their popularity on the web, Java applets still need to be rendered
obsolete by security flaws. Most browsers no longer support them, making
them a hazardous and obsolete technology.
Here's a brief overview if you must run an applet, however:
Prepare your applet as follows: Ensure that you have the Java
Running Environment installed, then compile your code and include
it within the HTML code file.
Make the HTML: Enter the parameters, required resources, and.
class name using the <applet> tag.
Execute the HTML: Check if it works by opening it in a browser that
remains compatible with applets and accepting prompts.
How To Run An Applet Using The Appletviewer Tool?
Though they are no longer as popular on the web, Java applets may still
be used with the help of the handy applet viewer application. It can be
used as follows:
Prepare to Install Java and locate the—class file from your generated
applet.
Gather around, command line! To access the directory containing
the.class file, open your terminal, browse there, and write an applet
viewer. The applet window pops up!
Run applets from HTML files using applet viewer, see
troubleshooting information with -debug, and customize with the -J
switch.
Get access and complete hands-on experience on a plethora of software
development skills in our unique Job Guarantee bootcamp. Get job-ready
with HackerEarth and HIRIST by enrolling in our comprehensive Full Stack
Java Developer Masters program today!
Conclusion
To summarize, the Java applet idea has played an important part in the
development of the web. When applets were first introduced, they offered
a method to add dynamic and lightweight elements to web pages. They
also allowed users to add interactive and eye-catching material straight
into their browsers. But as web technologies have advanced and security
concerns have grown, applets have lost some of their luster in
contemporary Java programming.
While the world of Java applets is gone, the invaluable lessons learned
from its time are carried forward by developers who continue to seek out
fresh and creative ways to solve problems as technology progresses.
If you are looking to enhance your Java skills further, we would highly
recommend you to check Simplilearn’s Full Stack Java Developer program.
In collaboration with IBM, this program can help you gain all the right skills
and make you job-ready.
If you have any questions or queries, feel free to post them in the
comments section below. Our team will get back to you at the earliest.
FAQs
1. What are the alternatives to Java applets for web development?
The alternatives to applet in Java for web development are:
JavaScript
WebAssembly
WebGL
Web Components
SVG and Canvas
2. Are Java applets still used today?
Java applets have seen a considerable decrease in application and
acceptance over the years. Several causes, including safety-related
issues, can be attributed to the fall.
3. Are there any security risks associated with Java applets?
Java applets have indeed been linked to vulnerabilities in security. They
contain flaws that bad players could use to inject illegal code into an
individual's computer.
4. Can I run Java applets on mobile devices?
It may not be easy to run Java applets promptly on contemporary mobile
devices. Many mobile phones, such as iOS and Android, do not support
Java applets because of safety considerations and the move toward
enhanced security and efficiency technologies.
Java Applets
Applets in Java are small and dynamic internet-based programs. A Java
Applet can be only executed within the applet framework of Java. For an
easy execution of applets, a restricted ‘sandbox’ is provided by the applet
framework. Generally, the applet code is embedded within an HTML page.
The applet codes are executed when the HTML pages get loaded into the
Java-compatible web browsers. Applets are mainly downloaded on remote
machines and used on the client side.
A Java applet can be a fully functional Java application as well since it can
utilize a complete Java API at its own accord. But still, there is a thin line
between an applet and an application in Java.
In the next section of this article on Applets in Java, I will be listing down
the differences between a Java Applet and a Java application.
Java Applet vs Java Application
Java Application Java Applet
Java Applications are the stand- Java Applets are small Java programs
alone programs which can be which are designed to exist within
executed independently HTML web document
Java Applications must have main() Java Applets do not need main() for
method for them to execute execution
Java Applets cannot run
Java Applications just needs the JRE
independently and require API’s
Java Applications do not need to Java Applets must extend
extend any class unless required java.applet.Applet class
Java Applications can execute Java Applets Applications cannot do
codes from the local system so
Java Applications has access to all
Java Applets has access only to the
the resources available in your
browser-specific services
system
Now that you know, how a Java applet differs from a Java application, let
me show you how to create a basic applet in Java through the next section
of this Java Applets Tutorial.
Applet Hierarchy in Java
class java.lang.Object
class java.awt.Component
class java.awt.Container
class java.awt.Panel
class java.applet.Applet
As displayed above, the Java Applet class which is a class of applet
package extends the Panel class of awt package. The Panel class is a
subclass of the Container class of the same package. The Container class
is an extension of Component class belonging to the same package. The
Component class is an abstract class and derives several useful classes
for the components such as Checkbox, List, buttons, etc.
Now that you know about the complete hierarchy of the Java Applet class,
let’s now try creating a simple Java Applet.
Java Applet Program
Below I have written a simple Java applet program which will simply
display the welcome message.
Upcoming Batches For Java Course Online
Course Name Date Details
Class Starts on 1st March,2025
Java Course Online View Detail
SAT&SUN (Weekend Batch)
EduApplet.java
1import java.applet.Applet;
2import java.awt.Graphics;
4//Extending the Applet class
5public class EduApplet extends Applet{
6public void paint(Graphics g){
7g.drawString("Welcome To Edureka's Applet Tutorial",150,150);
8}
9}
By now you are familiar with applets and know how to create them. In the
next section of this Java Applet Tutorial, I will be showing how to execute
an applet in Java.
Executing Applets in Java
By now I have demonstrated how to create an applet, but how do you
execute them? Well, unlike Java programs, executing applets is a bit
different process. Since applets are net based applications they need a
special environment to execute. Java provides two standard way to
execute an applet:
1. Using a Java-Enabled Web Browser
If you are trying to execute your Applet in this way, first you need to
compile your Java Applet file. Once done, you have to create a separate
HTML file and add the applet code within it with the reference to
your .class file within it. Now you can click the HTML file to launch the
applet in the browser. Below I have given the code required to create the
HTML file:
appletDemo.html
1<html>
2<body>
3<applet code="EduApplet.class" width="300" height="300">
4</applet>
5</body>
6</html>
2. Using appletviewer
In order to execute a Java Applet in this way, all you need to do is, instead
of creating a separate file for HTML code, you can directly add comment
at the beginning of your Java source code file indicating the presence of
APPLET tag within. This helps in documenting your Java code with a
prototype of the necessary HTML statements. This been done, now you
can execute your applet just by starting the Java Applet Viewer which
comes by default with JRE. When using this way of execution, your source
code should look like below:
EduApplet.java
1 import java.applet.Applet;
2 import java.awt.Graphics;
4 /*
5 <applet code="EduApplet" width=200 height=60>
6
8
</applet>
9
*/
1
0
1 public class EduApplet extends Applet{
1
public void paint(Graphics g){
1
g.drawString("Welcome To Edureka's Applet Tutorial",150,150);
2
}
1
3 }
Note: You can also install any IDE such as Eclipse, and execute your codes
directly from there itself.
Now that you know what is Java Applet and how it is executed, let’s dive
deeper into Java Applets Tutorial and get familiar with the lifecycle of the
Java Applets in the next section of this Java Applet Tutorial.
Programming & Frameworks Training
Full Stack Web Development Internship Program
Full Stack Web Development Internship Program
Reviews
5(11300)
Java Course Online
Java Course Online
Reviews
4(30350)
Python Scripting Certification Training
Python Scripting Certification Training
Reviews
5(5700)
Flutter App Development Certification Course
Flutter App Development Certification Course
Reviews
5(4800)
Spring Framework Certification Course
Spring Framework Certification Course
Reviews
5(4850)
Comprehensive Java Course Certification Training
Comprehensive Java Course Certification Training
Reviews
5(7250)
Node JS Course with Certification
Node JS Course with Certification
Reviews
5(3850)
Data Structures and Algorithms using Java Internship Program
Data Structures and Algorithms using Java Internship Program
Reviews
5(12050)
PHP & MySQL with MVC Frameworks Certification Training
PHP & MySQL with MVC Frameworks Certification Training
Reviews
5(1950)
Next
Applet Life Cycle
Every Java Applet needs to go through a series of phases from
initialization to destruction in order to complete its execution. For that, the
very first step is to inherit the java.applet.Applet class. This class aids with
various methods which help in holding up a basic framework for the Java
Applets. The various methods involved in the life cycle of Java Applet has
been depicted by the below diagram.
As you can see, there are 4 main methods which are mandatory for any
Java Applet to override. Let me brief you about each of these methods one
by one.
1. public void init(): This is the very first method to be invoked
during the life cycle of an applet. In this method, the variable that
will be used further in the applet is initialized. One thing you must
note here is that this method can be invoked only once per applet
life cycle.
2. public void start(): This is the second method that is invoked just
after the init() method is called by the browser. Each time a user
revisits the web page containing the applet, start() method is
invoked and the applet is started.
3. public void stop(): This method is invoked whenever a user leaves
the web page containing applet. In other words, the stop() method is
used to suspend the threads which are not required when the applet
is in the background or is not visible on the screen. These can be
easily resumed using the start() method.
4. public void destroy(): Finally, we have the destroy() method
which is invoked in order to completely remove an applet from the
memory. This method is invoked only once per applet life cycle and
all the engaged resources must be freed up before this method is
called.
One more method that is mostly used along with the above four is paint().
public void paint(Graphics g): This method is invoked whenever
an applet needs to be redrawn or repainted in the browser,
irrespective of the cause. The paint() method takes one Graphic
object as a parameter that contains the graphics context in which
the applet is being executed. Also, this method is invoked each time
output is expected from the applet.
Below is the basic skeleton of a Java Applet with all the life cycle methods.
AppletLifeCycle.java
1 import java.applet.*;
3 public class AppletLifeCycle extends Applet{
4 public void init(){
5 System.out.println("Applet is Initialized");
6 }
7 public void start(){
8
1
0
1
1
1
2
1
3
1 System.out.println("Applet is being Executed");
4
}
1
public void stop()
5
{
1
6 System.out.println("Applet execution has Stopped");
1 }
7
public void paint(Graphics g)
1
{
8
System.out.println("Painting the Applet...");
1
9 }
2 public void destroy()
0
{
2
System.out.println("Applet has been Destroyed");
1
}
2
2 }
Let’s now put whatever we have learned in this tutorial together and try to
create an applet which can respond to user actions.
Java Applet Demo
Below I have included a small and simple Java Applet program where you
will see how event handling works while an applet courses through its life
cycle.
AppletEventHandling.java
1 import java.awt.event.MouseListener;
2 import java.awt.event.MouseEvent;
3 import java.applet.Applet;
4 import java.awt.Graphics;
5 import java.awt.Color;
6 import java.awt.Font;
8 public class AppletEventHandling extends Applet implements MouseListener {
9 StringBuffer strBuf;
1
0
public void init() {
1
addMouseListener(this);
1
strBuf = new StringBuffer();
1
2 addItem(" Initializing the applet");
1 addItem(" Welcome to the Edureka's Applet Tutorial");
3
}
1
4
public void start() {
1
5 addItem(" Starting the applet ");
1
6
}
1
7
public void stop() {
1
8 addItem(" Stopping the applet ");
1
9
}
2
0
2
1 public void destroy() {
2 addItem(" Destroying the applet");
2
addItem(" Good Bye!!");
2
3
}
2
4
2 void addItem(String word) {
5
System.out.println(word);
2
strBuf.append(word);
6
repaint();
2
7 }
2
8
public void paint(Graphics g) {
2
9
g.drawString(strBuf.toString(), 10, 20);
3
0
3 setForeground(Color.white);
1
setBackground(Color.black);
3
2
}
3
3
3 public void mouseEntered(MouseEvent event) {
4
}
3
public void mouseExited(MouseEvent event) {
5
}
3
6 public void mousePressed(MouseEvent event) {
3 }
7
public void mouseReleased(MouseEvent event) {
3
8
3 }
9
public void mouseClicked(MouseEvent event) {
4
addItem(" Mouse is Clicked!!");
0
}
4
1 }
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
Next step is to compile the above .java file into .class file. Once done, now
you need to create an HTML file and add the reference of the .class file in
the applet tag. Below I have demonstrated how to write the code for this
HTML file.
eventHandling.html
Java Course Online
Weekday / Weekend BatchesSee Batch Details
1<html>
2 <title>Event Handling</title>
3 <applet code = "AppletEventHandling.class"
4 width = "300" height = "300">
5 </applet>
6</html>
When you execute this code, your applet should look like the below-shown
screenshot.
N
ow, to check whether your applet is working perfect or not, you can check
in your terminal. There you can see, all the phases your aplet is going
through with your every action. Below I have attached a screenshot for
the same.