90 likes | 118 Views
Applets. The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages. What is an applet. An applet is a subclass of Panel It is a container which can hold GUI components It has a graphics context which can be used to draw images
E N D
Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages
What is an applet • An applet is a subclass of Panel • It is a container which can hold GUI components • It has a graphics context which can be used to draw images • An applet embedded within an HTML page • Applets are defined using the <applet> tag • Its 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
Applications and Applets • Java applications are executed from the command line • A Java VM must be installed • The VM is given the name of a class which contains a main() method • The main method instantiates the objects necessary to start the application • Applets are executed by a browser • The browser either contains a VM or loads the Java plugin • The programmer must implement a class which is a subclass of applet • There is no main method. Instead, the applet contains an init() method which is invoked by the Browser when the applet starts
HTML 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>
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 } }
Applet Security • Java applets execute within a Sandbox • Applets cannot access the local file system • Applets cannot connect to systems other than the server from which they were downloaded • Applets cannot listen for inbound network connections • Applets cannot spawn processes or load local jar files • Applets cannot terminate the Java VM • Applets cannot change the security policy • Signed applets can be granted more access rights • These rights are controlled by the SecurityManager
Browser Issues • Unfortunately, Applets are not heavily used • Browsers support is limited • Browsers often contain outdated or buggy Java virtual machines • Each browser has its own compatibility issues. This usually means that the programmer has to implement workarounds for the various browsers • Microsoft 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.