Contents / Previous / Next


Applets

An applet is a Java program to be included in HTML pages and executed in Java-enabled browsers.

For security reasons applets are limited in what they can accomplish, because they can be executed by anybody via the Internet.


Implementing Applets

Every applet is implemented by creating a subclass of the applet class (or class JApplet):

The functionality applets inherit from the Applet class ranges from communication with the browser to the ability to present a graphical user interface (GUI).

Example, "Hello World" as applet:

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorldApplet extends Applet 
{
    public void paint(Graphics g) 
    {
        g.drawString("Hello world!", 50, 25);
    }
}
Or using Swing:
import javax.swing.*;
import java.awt.*;

public class HelloWorldApplet extends JApplet 
{
  public void init() {
    getContentPane().add(new JLabel("Hello World!"));
  }
}

The HelloWorld applet implements just one method, the paint method.

The Graphics object passed to the paint method represents the applet's on-screen drawing context.

When a Java-capable browser loads a page containing an applet, the browser sends a request to the applet, telling the applet to initialize itself and start executing.

An applet is not restricted to the applet-subclass, it can define additional custom classes.


Applet Methods (Life Cycle)

Unlike Java applications, applets do not need to implement a main method.

Instead every applet must implement at least one of the following methods:

paint(): Called whenever the applet must paint itself. For animations paint is called continuously (via repaint) from an animation thread.

init(): Called to perform first-time initialization of the applet.

start(): Called every time the applet moves into sight on the Web browser to allow the applet to start up its normal operations (especially those that are shut off by stop()). It is also called after init().

Applets can implement two more methods that the browser calls when a major event occurs (such as leaving the applet's page):

stop(): Called every time the applet moves out of sight on the Web browser to allow the applet to shut off expensive operations. It is also called right before destroy().

destroy(): Called when the applet is being unloaded from the page to perform final release of resources.

Applets can implement any number of other methods, as well.


Running an Applet

Applets are meant to be included in HTML pages.
Using the <APPLET> tag, you specify the location of the Applet subclass and the dimensions of the applet's on-screen display area: <HTML> <HEAD> <TITLE>Page with Applet</TITLE> </HEAD> <BODY> Here is the output of the applet: <APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25> </APPLET> </BODY> </HTML> Now you can load the page with the applet into a browser that understands Java.

If you include an applet twice in one page, the browser loads the class file once and creates two instances of the class.

Starting from HTML 4.0 the <APPLET> tag is deprecated, it is replaced by <OBJECT>. But almost all browsers still understand <APPLET>, whereas <OBJECT> is not yet widely spread.

Alternatively to a browser you can invoke Sun's 'appletviewer' with the command:

appletviewer HelloWorldApplet.html


Applet Restrictions

You can generally answer the question of what an applet is able to do by looking at what it is supposed to do: extend the functionality of a Web page in a browser.

An applet cannot touch the local disk.
This means writing or reading, since you would not want an applet to read and transmit private information over the Internet.
It also cannot not execute local files.

An applet cannot make network connections except to the host that it came from.

Java offers digital signing for applets. Many applet restrictions are relaxed when you choose to allow signed applets (those signed by a trusted source) to have access to your machine.

Each browser has a SecurityManager object that implements its security policies. When a SecurityManager detects a violation, it throws a SecurityException. Your applet can catch this SecurityException and react appropriately.


Passing Parameter to Applets

You can place parameters within an applet tag to provide information that the applet can retrieve: <APPLET CODE="GetParameterApplet.class" WIDTH=150 HEIGHT=25> <param name="firstname" value = "Fred"> </APPLET> There can be as many parameters as you want.

An applet uses the 'getParameter()' method to access parameter values:

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

public class GetParameterApplet extends Applet 
{
    private String firstName;

    public void init()
    {
	firstName = getParameter( "firstname" );
    }

    public void paint(Graphics g) 
    {
	String Greetings;
	Greetings = "Hello "+firstName+" !";
	Font big = new Font("Serif", Font.BOLD, 24);
	g.setFont( big );
        g.drawString( Greetings, 30, 20 );
    }
}


Evaluating HTML Forms with Applets

Nop! The applet runs on the client-side and an HTML form has to be evaluated on the server-side.

...turn the page to servlets.