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.
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.
A web page can contain two types of 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.
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).
An applet can be run two ways:
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>
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);}}
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.javaimport 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>*/
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.Applets have many benefits. They are as follows:
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.
Free Resources