Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

html
java
java applet
communitycreator

What is a Java applet?

Innocent Ekweogu

A Java applet is a Java program that can embed in web pages. It runs in a web browser and works on the client-side. We can embed an applet in the HTML page using the <applet> or <object> tags and hosted on the webserver.

Applets provide web applications with interactive functionality that is not possible with only HTML. In response to user operations, an applet can change the graphic content provided. It makes an applet suitable for demonstration, display, and teaching.

A brief history of the Java applet

The first version of the Java language, released in 1995, introduced Java applets. Major web browsers began to remove support for the underlying technology applets used from 2013. Applets became completely non-executable by 2015 – 2017.

Support for running applets in the browser is only possible when browser vendors commit to using standards-based plugins. Since this is no longer happening, applet support ended in March 2019. Java applets had deprecated as of Java 9 in 2017.

Types of applets

A web page can contain two types of applets:

  1. Local applet
  2. Remote applet

Local applets

Local applets are developed and stored locally, and therefore do not require an Internet connection to be located because the directory is located on the local system.

A local applet

Remote applets

Remote applets are stored in a remote computer and an Internet connection is needed to access them. The remote applet is designed and developed by other developers. To find and load a remote applet, you need to know the address of the network applet, i.e., the Uniform Resource Locator (URL).

A remote applet

How to run an applet

An applet can be run two ways:

  1. Through HTML file.
  2. Using the appletviewer tool (for testing purposes).

1. Through HTML file:

For an applet to run in a web browser, we must write a short HTML text file that contains a tag to load an applet. For this, we can use <applet> or <object> tags. The HTML <applet> tag specifies an applet. It embeds Java applets in HTML documents. It does not support HTML5.

<!DOCTYPE html>
<html>

   <head>
      <title>HTML applet Tag</title>
   </head>

   <body>
      <applet code = "newClass.class" width = "300" height = "200"></applet>
   </body>
	
</html>
A sample applet by HTML file

In the HTML text file above, the code attribute of the <applet> tag specifies the applet class to execute. The width and height attributes are also required. They define the initial size of the panel on which the applet is running. The applet command must be closed with the </applet> tag. Below is the applet class embedded in the HTML file above:

import java.applet.*;
import java.awt.*;

public class newClass extends Applet {
   public void paint (Graphics gh) {
      gh.drawString("Tutorialspoint.com", 300, 150);
   }
}
Here is the newClass.java file

2. Using the appletviewer tool:

The appletviewer runs an applet in the window. It is usually the fastest and easiest way to test an applet. Create an applet containing the <applet> tag in the comment, and compile it. It is for testing purposes only.

//First.java  
import java.applet.Applet;  
import java.awt.Graphics;  
public class First extends Applet{  
  
    public void paint(Graphics g){  
        g.drawString("welcome to applet",150,150);  
    }  
  
}  
/* 
<applet code="First.class" width="300" height="300"> 
</applet> 
*/ 
A simple example of an Applet by appletviewer tool

To run an applet using the appletviewer tool, write in the command prompt:

c:\>javac First.java

c:\>appletviewer First.java

  • javac is the compiler that compiles java codes using a command line.
  • First.java is the applet class to be tested.
  • appletviewer is a tool that tests the applet class.

Advantages of applets

Applets have many benefits. They are as follows:

  1. It works on the client-side, so the response time is shorter.
  2. It is secured. Applets have strict security rules that apply to web browsers.
  3. It runs on browsers on multiple platforms, including Linux, Windows, and Mac Os.

Disadvantages of applets

  1. The client browser requires a plugin to run the applet.
  2. The mobile browser on iOS or Android does not run any Java applets. Desktop browsers have dropped support for Java applets along with the rise of mobile operating systems.

Applet features over HTML

  1. It displays the dynamic web pages of a web application.
  2. Play audio files.
  3. Display documents.
  4. Play animations.

Conclusion

Java applets represent an unsuccessful first attempt to bring an interactive experience to the Web. Today, Chrome has completely dropped support for applets. Why interactive Web has moved away from the applet in favor of JavaScript is a matter of debate. The fact that applets require software to be installed outside of the browser and special browser plugins to work could be one of the reasons.

RELATED TAGS

html
java
java applet
communitycreator
RELATED COURSES

View all Courses

Keep Exploring