Java Programming/Applets/Overview

A Java applet is an applet delivered in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Oracle's AppletViewer, a stand alone tool to test applets. Java applets were introduced in the first version of the Java language in 1995. Java applets are usually written in the Java programming language but they can also be written in other languages that compile to Java bytecode such as Jython.

Applets are used to provide interactive features to web applications that cannot be provided by HTML. Since Java's bytecode is platform independent, Java applets can be executed by browsers for many platforms, including Windows, Unix, Mac OS and Linux. There are open source tools like applet2app which can be used to convert an applet to a stand alone Java application/windows executable. This has the advantage of running a Java applet in off-line mode without the need for Internet browser software.

The Java applet is less and less used. In 2018, you can use it only from Microsoft Edge's "Compatibility mode" and Firefox Extended Support Release. You'd rather use JavaScript when it is possible.

First applet
The two things you must at least create is an HTML page and a Java class. It can be done on a local folder, no need to run a server but it will be harder to understand what is local, what is remote. The HTML page has to call the Java class using the  markup:

Save this file on a folder. As the  markup is calling a Java class called , our class should be called  :

Save this file and compile the class on the same folder. Now let's open the web page on a browser: We clearly see that "Hello, world!" is not rendered the same way as the rest of the page.

HTML code

 * See also applet markup.

To embed an applet in a HTML page, you have to insert a  markup. This markup can have several attributes:

The attributes with * are mandatory.

There have been some discussions about the usage of  tag but it still can be used for beginning and also would work in the real world as well.

Java source code
Applets are not constructed in the same way as other classes or main programs. The entry point is different and the main class should extend the  class. The  class has four methods that can be called by the browser and you can redefine:

The four first methods define the lifecycle of an applet. At least  or   must be redefined. The HTML applet tag can be embedded in the applet source code to allow the applet to be run directly by a simple applet viewer, without the need for an .html file. Typically, the applet tag immediately follows the import statements. It must be enclosed by  comments: