130 likes | 241 Views
G6DICP - Lecture 27. Java Applets. Types of Java programs. Applications Standalone programs Applets Delivered over WWW to client (browser) Servlets Run on WWW server. Java Application. Local Computer. Bytecode (.class). Interpreter (java or jre). VM. Java Applet. Server.
E N D
G6DICP - Lecture 27 Java Applets
Types of Java programs • Applications • Standalone programs • Applets • Delivered over WWW to client (browser) • Servlets • Run on WWW server
Java Application Local Computer Bytecode(.class) Interpreter(java or jre) VM
Java Applet Server Bytecode(.class) Client VM Web Browser
Java Servlet Web Server(eg Apache with jserv) Bytecode(.class) HTML VM
Feasibility of Applets • Java has platform independent binaries • WWW is designed to be data neutral • Security is the major issue
The HTML APPLET Tag <HTML> <HEAD> <TITLE>An Applet Test</TITLE> </HEAD> <BODY> <H1>An Applet Test</H1> <APPLET CODE=”myApplet.class" WIDTH=360 HEIGHT=250> </APPLET> </BODY> </HTML>
About Applets • Applets are always graphical • Applets must be subclasses of the Applet class (Panel) • The Applet container is drawn in the window of the web browser. • Class Applet contains the init() method • Invoked automatically by a browser when a web page containing the <APPLET> tag is loaded • Thus main method is not needed(!) • Security issues • Signed Applets - full functionality of Java • Unsigned Applets - substantially restricted functionality
A Hello World Applet import java.awt.*; import java.applet.*; public class demo extends Applet { public void init() { setSize(360,250); } public void paint(Graphics g) { g.drawString("Hello World!",50,50); } }
Typical Conversion of an Application to an Applet • Make sure that all i/o goes through the AWT interface. • eg use g.drawString() instead of System.out.println() • Remove any means for stopping the program • eg remove System.exit() • Subclass Applet instead of Frame • Override the Frame.init() method, and put the functionality of the applications constructor in there. • Remove the main() method of the application.
Applet Security Restrictions (1) • Applied by web browser (ie do not usually apply in appletviewer) • Cannot use native code • Cannot read some system properties • Cannot start other programs • Applet windows are clearly labelled - this cannot be disabled or removed.
Applet Security Restrictions (2) • Cannot read or write files on the client. • No direct file access to the server (although network connections can be made). • Network connections can only be made to the machine from which the applet is being served.
Version Issues • Classes used must be available • This can be a problem with Swing • Can be serious compatibility problems between software developed with a newer JDK and on earlier browser VM • Modern browsers usually have a “plugin” VM • Many older ones do not