Software Technologies for the Web

Applets

An applet is a Java application that is executed through a web browser, such as Firefox or Internet Explorer using their Java Virtual Machine (JVM). A Java applet has the advantage of it is compiled using Java classes and are hidden from the browser, Javascript is embedded into the HTML document and can been viewed as part of the source code (Dawson 2008).

Using Applets

There are three ways in which you (the developer) can incorporate an applet into your HTML page.
These are;

<applet>

<applet code=Applet1.class width="200" height="200">
Your browser does not support the applet tag.
</applet>

This method will execute an applet in all browsers, which seems to a good enough reason to use it. However the W3C have declared it to be outdated and are pressing for the use of the object tag instead. Unfortunately not all browsers support the object tag, so it is the recommendation of Sun Microsystems that developers continue to use the applet tag (Sun Microsystems 2008).

<embed>

<embed code="Applet1.class"
width="200" height="200"
type="application/x-java-applet;version=1.6.0"
pluginspage="http://java.sun.com/javase/downloads"/>

The embed tag works with the Mozilla family of browsers (e.g. Firefox). They reject the classid attribute of the object tag. The type attribute can be declared in one of two ways to call the right JRE. By declaring it as type="application/x-java-applet;version=1.6.0" calls the most up-to-date version that is installed on the client machine, and will use it as long as it is newer then the one declared by.
The other choice is to specify the JRE to use, e.g. type="application/x-java-applet;jpi-version=1.6.0_01 The first option seems preferable as it can be used by a wider variety of JRE's.
In either case if the version argument isn't fulfilled it will direct the user to the URL as specified by the pluginspage attribute. This has the advantage of at least it helps the user to find the plugin required for the applet to work (Sun Microsystems 2008).

<object>

<object
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="200" height="200">
codebase="http://java.sun.com/update/1.6.0/
 jinstall-6-windows-i586.cab#Version=6,0,0,99">
<param name="code" value="Applet1.class">
</object>

As with the embed tag, the object tag has only a loyalty to Internet Explorer. It provides similar functionality; The classid attribute is used to declare the version of the JRE required to execute the applet. The problem with the classid is that it isn't readily understandable. If the version isn't good enough then the codebase attribute works in the same way as to the pluginspage attribute, telling the user where to obtain the right version of the JRE for a windows system (Sun Microsystems 2008).

HTML Converter

There is a solution to making sure the right tags are used in your web pages. You could write some Javascript to detect the browser type and based on this format the tags accordingly. Sun have packaged an application called HTMLconverter as part of the Java SDK. This application will convert files passed to it based on choices entered by the user. The conversion of the files comes in three basic flavours, based on the target architecture. Goto here for more details.

HTMLconverter example

Input choices to HTMLconverter (we are going for full compatibilty).

Back to top

Security and the Security Manager

The Java Sandbox

When a user runs an applet it is effectively downloading code from the Internet to run on the local machine. The user can not guarantee that they code they are about to execute is safe. When downloaded an applet is placed in a ‘sandbox’. This is an environment that encapsulates the applet, separating it from the host browser. The sandbox is constructed form three elements;

The Byte Code Verifier
this verifies the byte code of an applet, ensuring that it abides by a set of rules. These include accessing restricted access rules like opening ports (needed to connect to a remote database).
Applet Class Loader
this part of the sandbox is used to establish when and how an applet is allowed to add classes to a Java environment. An example of this is if an applet tries to replace code of the runtime environment with its own code.
Security Manager
the security manager restricts the ways that an applet is allowed to use interfaces. It contains the Security Exception routines, which can stop various parts of the code from executing.

(Mcgraw and Felten 1997)

Security Manager

Applet viewers (this includes the SDK applet viewer and web browsers) have a SecurityManager object. The role of this is to check the applet for violations in it's security. If one is detected it will display an error message by throwing a SecurityException object. This error is then displayed to STDOUT (normally the user screen). Developers can, if they want to, catch these errors to prevent the applet from crashing and invoke other methods.

Applet viewers enforce the following restrictions (taken from Sun Microsystems 2008;

Applets cannot load libraries or define native methods.
Applets can use only their own Java code and the Java API the applet viewer provides. At a minimum, each applet viewer must provide access to the API defined in the java.* packages.

An applet cannot ordinarily read or write files on the host that is executing it.
The JDK Applet Viewer actually permits some user-specified exceptions to this rule, but older browsers generally do not. Applets in any applet viewer can read files specified with full URLs, instead of by a filename. A workaround for not being able to write files is to have the applet forward data to an application on the host the applet came from. This application can write the data files on its own host.

An applet cannot make network connections except to the host that it came from.
The workaround for this restriction is to have the applet work with an application on the host it came from. The application can make its own connections anywhere on the network.

An applet cannot start any program on the host that is executing it.
Again, an applet can work with a server-side application instead.

An applet cannot read certain system properties.
Applets are forbidden to access details about the user's home directory and account.

Windows that an applet brings up look different than windows that an application brings up.
You can identify the Applet window by the name 'Java Applet Window', which is displayed at the bottom of the window. Application window would not have any name at its bottom. This helps the user distinguish applet windows from those of trusted applications.
Back to top

Example Applets

AppletOne

The first applet example is a simple affair, it displays two labels with coloured backgrounds.
To get the background colour the applet takes two parameters passed in via the HTML page. We do this by using the following code:

<param name="colourOne" value="3">

And process them in the applet using this code;

String colourOne = getParameter("colourOne");

Note that the parameter passed is set as a string, if you want to convert it to another type (as we do) you need to cast it from a string.
Because we are using a 'switch' statement we need to convert the input into either an integer or single character input. This code casts the string into an integer.

// cast parameter input to int for switch statement
int colOne = Integer.parseInt(colourOne);

Once we have our input ready, we pass it through the switch statement to set the label background. All that is left to do now it create a layout to put the elements into and display them. There are many layouts that you can use, this applet uses a grid layout. When you use this type of layout you need to declare the rows and columns you want, then just add the elements to the layout.

// define layout
// setup container for grid layout
Container c = getContentPane();
// set rows, columns
c.setLayout(new GridLayout(1, 2));
// add elements to grid
c.add(jLabel1);

Now we have had a look at the code, lets have a look at the applet.

Stand-alone example here.
Source code for HTML page calling applet is here.
Source code for this applet is here.

AppletTwo

This applet does a similar thing to the previous one, but this time we are going to get the input from the user. We are going to use a method from the swing library called JColorChooser. This is a GUI element that allows the user to pick a colour from a palette. This also means we will need to add some code to react to a button click (known as an event action listener). Also, in an effort to keep the code tidy, we are going to keep all the source for this in a package. We do this by declaring it as a package at the start of the script.

package applets;

Next we need to create the buttons to be used.

jButton1 = new javax.swing.JButton();

Then code what will happen when it is clicked. This is where we will implement the JColorChooser, and set the background.

jButton1.addActionListener(new       java.awt.event.ActionListener()
{
// what to do when action, ie when clicked
public void actionPerformed(java.awt.event.ActionEvent evt)
{
// get colour from colour chooser
Color initialBackground = jButton1.getBackground();
Color background = JColorChooser.showDialog(null,    "JColorChooser Sample", initialBackground);
// if colur has been selected, set colour
if (background != null)
{
jLabel1.setBackground(background);
}
}
});

And below is the result of all this coding.

Sorry, applet not working.

Stand-alone example here.
Source code for HTML page calling applet is here.
Source code for this applet is here.

Back to top

References

Back to top