Loading, please wait...

A to Z Full Forms and Acronyms

What is Applet in Java?

Jun 11, 2020 Java, JavaApplet, 7319 Views
Brief about Applet

What is Java Applet?

The applet is a special program in Java. It can be embedded into a web page. It runs in a web browser. It works on a client-side. They are used to generate dynamic content. It is a fully functional small size Java application.

Key Points:

  • java.applet.Applet is the main class of all the sub-classes of the applet.
  • Applets run within a web browser or an applet viewer. The applet viewer is the standard applet tool that is provided by the JDK.
  • Applets execution does not begin with a main() method.
  • The output of an applet window is handled by the methods of AWT such as drawString() instead of System.out.println().
  • They are designed to embed into an HTML page.

Life Cycle of an applet:

It is important to understand the life cycle of an applet before working on an applet. When we start an applet init() and start() method are called.

  • init(): It is the first method to be called in a life cycle of an Applet. This method is called only once to load the applet class.
  • start(): it is called after init() method. It is called to start an applet. It needs to call whenever the applet has been stopped to restart it.

To terminate an applet, stop() and destroy() method are called.

  • stop(): It is used to stop the Applet. This method is called when a web browser leaves the HTML document i.e browser is minimized.
  • destroy(): It is used to destroy the object of the Applet. It is called to free up the memory space when no longer needed. Like, init() method, it is also called only once.

There is one more method paint() which is always called when AWT- based output is redrawn. It contains one graphic parameter that describes the graphical environment in which the applet will be running.

Java plug-in software manages a life cycle of an applet.

There are two ways to run an Applet.

  • With the help of an HTML file.
  • With the help of appletViewer tool.

Example of Applet by HTML file

//demo.java

import java.applet.Applet;  
import java.awt.Graphics;  
public class demo extends Applet{  
public void paint(Graphics g){  

g.drawString("HELLO",100,100);  
}  

}
<html>  
<body>  
<applet code="demo.class" width="400" height="400">  
</applet>  
</body>  
</html> 

 In the above example, create an applet and compile it. Now create an HTML file and place an Applet code in the HTML file. In the end, execute the HTML file to run an applet.

Example of the applet viewer tool

   // example.java

import java.applet.Applet;  
import java.awt.Graphics;  
public class example extends Applet{  
public void paint(Graphics g){  
g.drawString("Hello World",100,100);  
}  
}

/* 

<applet code="example.class" width="400" height="400"> 

</applet> 

*/  

Now write in the command prompt, to execute it by applet viewer tool

c:\>javac example.java

c:\>appletviewer example.java

In the above example, create an applet that contains an applet and compile it. Open the command prompt to execute it by applet viewer tool. The HTML file is not required in the way of executing an Applet.

Advantages of Applet:

  • It provides a secure two-way interaction between web pages.
  • It takes less time to respond because it works on the client-side.
  • It is more secure.
  • It is useful in creating a dynamic web page.

Disadvantages of Applet:

  • It can not read certain system properties.
  • It can not read and write ordinary files on the execution host.
A to Z Full Forms and Acronyms

Related Article