Sample Applet

In the Java Card Platform, Version 3.1, all applications must include a class that inherits from javacard.framework.Applet, which will provide an interface with the outside world.

This also applies to applications that are based on remote objects, for two main reasons:

  • The remote objects must be instantiated and initialized, which can be done in an applet's install method.

  • The remote objects must communicate with the outside world, which can be done in an applet's process method.

For conversion, an applet should be assigned with an AID known on the client side, 0x00;0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08, since this AID is used in the client program.

The following is the basic code for such an applet:

package examples.purse;

import javacard.framework.*;
import javacard.framework.service.*;
import java.rmi.*;

public class PurseApplet extends Applet {
    private Dispatcher dispatcher;

    private PurseApplet() {
        // Allocates an RMI service and sets for the Java Card platform
        // the initial reference
        RemoteService rmi = new RMIService(new PurseImpl());
        // Allocates a dispatcher for the remote service
        dispatcher = new Dispatcher((short) 1);
        dispatcher.addService(rmi, Dispatcher.PROCESS_COMMAND);
    }

    public static void install(byte[] buffer, short offset, byte length) {
        // Allocates and registers the applet
        (new PurseApplet()).register();
    }

    public void process(APDU apdu) {
        dispatcher.process(apdu);
    }
}