An Applet is the special type of Java program that is run on web browser. The Applet class provides the standard interface between applet and browser.
An applet class does not have main method and it extends the java.applet.Applet
class. An applet program runs through applet viewer.
Advantages of Applets
- Applets are supported by most web browser.
- Applets have very less response time, because it works at client side.
- Applets are quite secure because of their access of resources.
- An untrusted applet has no access to local machine.
- The size of applets is small, making it easier to transfer over network.
Limitations of Applets
- Applets require a Java plug-in to execute.
- Applets do not support file system.
- Applet itself cannot run or modify any application on the local system.
- It cannot work with native methods of system.
- An applet cannot access the client-side resources.
Applet Life Cycle
It is derived from the Applet class. When an Applet is created, it goes through different stages; it is known as applet life cycle.
1. Born or initialization state
2. Running state
3. Stopped state
4. Destroyed state
5. Display state
Applet Life Cycle Methods1. init ( )
The init( ) method is responsible for applet initialization when it is loaded. It is called by browser or applet viewer only once.
2. start ( )
public void init( )
It is invoked after the initialization state and applet entered into running state. It starts the execution of Applet and the applet becomes an active state.
3. paint( )
public void start()
This method is used to display output on screen. It takes java.awt.Graphics object as a parameter. It helps to create Applet GUI such as a colored background, drawing etc.
4. stop( )
public void paint(Graphics g )
This method is responsible to stop the execution and applet becomes temporarily inactive state. It is invoked when Applet is stopped.
5. destroy( )
public void stop( )
It destroys and removes the applet from the memory. It is invoked only once. This is end of the life cycle of applet and it becomes dead.
public void destroy()
Developing an Applet ProgramSteps to develop an applet program
1. Create an applet (java) code i.e. .java file.
2. Compile to generate .class file.
3. Design a HTML file with <APPLET> tag.
Running the Applet ProgramThere are two ways to run the applet program
1. By HTML
2. Appletviewer tool
Run the complete program:
Example : Getting a message printed through Applet
public class AppletDemo
public void paint(Graphics g)
g.drawString("Welcome to TutorialRide", 50, 50);
1. Save the above program: AppletDemo.java
2. Compile: > javac AppletDemo.java
<title> AppletDemo </title>
<applet code="AppletDemo.class" width="200" height="250">
> appletviewer AppletDemo.html
To execute the applet program through appletviewer
tool, write it on command prompt.
C :\> javac AppletDemo.java
C :\> appletviewer AppletDemo.java
Difference between Applications and Applets
|An application runs stand-alone.||An applet program runs under the control of browser.|
|It can access any data or software available on the system.||It cannot access anything on the system except browser’s service.|
|Execution start from the main() method.||Execution start from the init() method because main() is not available.|
|It can read and write to the file system.||Cannot read and write to the file systems.|
|Does not provide any security.||An applet provides the security features.|
|Application run using Java interpreter.||It runs using applet viewer or on web browser.|