Running an Java Applet in a Web Browser

Java become so popular because of their support for applets. Applets are small program that run inside the web browser using JVM (Java virtual machine).

They are added as a reference on web page using applet tag.

for example
<applet CODE=”HelloWorldApplet.class” WIDTH=150 HEIGHT=25>

There are following restrictions on Java applets.
1. Java applets cannot read or write to the disk on the local system.
2. Java applets cannot execute any programs on the local system.
3. Java applets cannot connect to any machines on the Web except for the server from which they are originally downloaded.

All applets are derived from java.applet.Applet class.

for example
public class HelloWorldApplet extends java.applet.Applet {

There are few major applet actions

Initialize, Occurs when applet is loaded/reloaded.
Destroying, It lets applet clean up before browser exits.
Painting, It helps in drawing things on screen.

import java.awt.Graphics;
public class HelloWorldApplet extends java.applet.Applet {
	public void paint(Graphics g) {
		g.drawString("Hello world!", 0, 0);

In the above code snippet, we overrides paint method and prints a string "Hello world!" on browser.

The applet doesn't actually print a couple of words to the screen, and there's not really anything to initialize i.e. start() or a stop() or an init() method are not mandatory while writing applets.

In paint method, graphics object is passed, which is actually responsible for printing things on applet.

To run an applet, we need to compile the applet and then include the .class file in the applet html element. We also need to have Java plugin installed and enabled in the browser.

Sample html file will look like
<TITLE>Hello world java applet!</TITLE>
<applet CODE=”HelloWorldApplet.class” WIDTH=150 HEIGHT=225></applet>

There are two major attributes of Applet element
1. CODE attribute to indicate the name of the class that contains your applet.
2. WIDTH and HEIGHT attributes to indicate the size of the applet.

There may be instances when the browser do not support applet element (same way as noscript element). In that case we can put some text within the applet opening and closing element and that text will be rendered when such a browser is encountered.

for example
<applet CODE=”HelloWorldApplet.class” WIDTH=150 HEIGHT=225>Please install JVM to run this Java Applet</applet>

We can also pass parameters to the applet using "param" elements (very similar to flash objects) and to access these parameters we can use getParameter method.

for example
<applet CODE=”HelloWorldApplet.class” WIDTH=150 HEIGHT=225>
<param name="text" value="This text is passed from browser" />
Please install JVM to run this Java Applet

To access these parameters

import java.awt.Graphics;
public class HelloWorldApplet extends java.applet.Applet {
	string text;
	public void init() {
		this.text = getParameter("text");
	public void paint(Graphics g) {
		g.drawString(this.text, 0, 0);
Add Comments :