Applets
description
Transcript of Applets
Applets
The objectives of this chapter are:
To describe applets and their purpose
To discuss embedding applets in HTML pages
An applet is a subclass of PanelIt is a container which can hold GUI componentsIt has a graphics context which can be used to draw images
An applet embedded within an HTML pageApplets are defined using the <applet> tagIts size and location are defined within the tag
The browser contains a Java Virtual Machine which executes the applet
The applet .class file is downloaded, through the net, into the Virtual machine.
• Unfortunately, most browsers have a very old version of the JVM• The standard is either Java 1.1.4 or 1.1.5• Sun has released a Java 1.2 plugin which can be used instead
What is an applet
Java applications are executed from the command lineA Java VM must be installedThe VM is given the name of a class which contains a main() methodThe main method instantiates the objects necessary to start the application
Applets are executed by a browserThe browser either contains a VM or loads the Java pluginThe programmer must implement a class which is a subclass of appletThere is no main method. Instead, the applet contains an init() method which is invoked by the Browser when the applet starts
Applications and Applets
The HTML applet tag contains the following parameters:
<Applet code="name of .class file"codebase="URL where code is loaded from"
name="applet identifier" align="LEFT|RIGHT|CENTER width="size in pixels" height="size in pixels“>
<param name=“aName1” value=“aValue”><param name=“aName2” value=“aValue”>
</Applet>
HTML and Applets
Example HTML file
<HTML><HEAD><TITLE> Sample Applet</TITLE></HEAD>
<BODY><APPLET code="Sample.class" WIDTH=100 HEIGHT=200>
This text will display if the browser does not support applets
</APPLET>
</BODY></HTML>
Sample Applet
import java.applet.*;import java.awt.*;
public class Sample extends Applet implements ActionListener{private Button okButton = new Button("OK");private Button cancelButton = new Button("Cancel");
public void init(){
okButton.addActionListener(this);cancelButton.addActionListener(this);add(okButton);add(cancelButton);
}
public void actionPerformed(ActionEvent x){
// ...}
...}
Passing Parameters To Applets
<Applet code="Menu.class“><param name="MenuName" value="My Web Site“><param name="Font" value="Serif">
</Applet>
public class Menu extends Applet{public void init(){
String menuName = getParameter("MenuName");String theFont = getParameter("Font");
// do something with parameters}
}
Java applets execute within a SandboxApplets cannot access the local file systemApplets cannot connect to systems other than the server from which they were downloadedApplets cannot listen for inbound network connectionsApplets cannot spawn processes or load local jar filesApplets cannot terminate the Java VMApplets cannot change the security policy
Signed applets can be granted more access rightsThese rights are controlled by the SecurityManager
Applet Security
Unfortunately, Applets are not heavily usedBrowsers support is limited
Browsers often contain outdated or buggy Java virtual machinesEach browser has its own compatibility issues. This usually means that the programmer has to implement workarounds for the various browsersMicrosoft has not been very cooperative in terms of ensuring that IE correctly implements applets. Even the Java plugin has issues under IE
Because the AWT and Swing are not highly regarded, use of Java in the client is minimal.
Browser Issues