340 likes | 346 Views
EE2E1. JAVA Programming. Lecture 10 Applets. Contents. Introduction Web-servers and clients A simple example “Hello World!” applet Converting an application to an applet Browsers and plug-ins Applet attributes Passing parameters to applets Applet security JAR files. Introduction.
E N D
EE2E1. JAVA Programming Lecture 10 Applets
Contents • Introduction • Web-servers and clients • A simple example “Hello World!” applet • Converting an application to an applet • Browsers and plug-ins • Applet attributes • Passing parameters to applets • Applet security • JAR files
Introduction • Java applets are programs written in Java which are run by a web browser such as Netscape or Internet Explorer • The applet code is downloaded from its host computer each time it is run • Applets are used for adding extra functionality to web pages • As we shall see, converting a normal Java application program to an applet is easy • The main problems stem from the browser incompatibility and security issues • Java applets are not the be all and end all of Java programming but they can be useful
Web-servers and clients • When we run a web browser to access the internet, the web browser program runs on a client computer • Usually the PC from which we are working • The .html web page we are accessing is on the Web Server computer • It is on this machine that the code (the Java .class file) for the Java applet resides which the .html file imports
.html web page MyApplet.class Client running Netscape Web server
Example – A “Hello World!” applet • The following example applet draws a “Hello World” string • http://www.eee.bham.ac.uk/spannm/Java%20Stuff/HelloWorldApplet/HelloWorldApplet.html • It is essentially a very simple Swing program • However, there is no main program • Instead there is an init() method within a sub-class of JApplet • We also need a .html file to tell the browser to download and run the applet
class HelloWorldPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Font f=new Font("SansSerif",Font.BOLD,36); g.setFont(f); g.drawString("Hello World!", 50, 120); } } public class HelloWorldApplet extends JApplet { public void init() { Container contentPane = getContentPane(); contentPane.add(new HelloWorldPanel()); } }
The only other thing required is a .html which tells the browser to load a particular Java class file containing the applet code (plus the size of the window in which to run the applet) <APPLET CODE = "HelloWorldApplet.class" WIDTH = 300 HEIGHT = 300 > </APPLET> • This simple .html file is fine if we don’t need a Java plug-in (see) later with our browser • OK for Netscape 6.2 and later versions • .html file more cumbersome for use with earlier versions of Netscape
Converting an application to an applet • An applet is essentially a graphics container (rather like the JFrame) class • The JApplet class replaces JFrame as the outer container public class HelloWorldApplet extends JApplet • JApplet is a sub-class of Container as is JFrame and contains many of the same methods
An applet has no main() method • This is replaced by the init() method and is called by the browser to perform initializations • Usually an applet has no constructor • The browser determines the graphics container title and the size is determined in the SIZE parameters of the .html file • No need for calls to setSize() and setTitle()
Other methods of JApplet that you may need to implement • start() • Automatically called by the browser after init() and when the user returns to the web page (whereas init() only called once) • stop() • Automatically called when the user moves off the web-page. • destroy() • Automatically called when the browser shuts down (after it calls the stop() method) • Normally put code to reclaim resources here
Lots of other example applets can be found at http://javaboutique.internet.com/applet_index/d.html • A favourite of mine is http://javaboutique.internet.com/Date2Day/
Browsers and plug-ins • Internet browsers such as Explorer and Netscape have not kept up with the development of Java • Both contain a Java Virtual Machine (JVM) but earlier versions of the browsers can only run Java 1.0 programs not the more recent Java 2 • This is always going to be a problem as Java is developing much faster than internet browsers • To combat his problem, Sun developed a Java plug-in which ‘plugs into’ either IE or Netscape • Allows the browser to run the JVM installed on the machine running the browser and not its internal version
Client computer Browser Java virtual machine Java virtual machine html file <… code for plug-in …>
Plug-ins allow the user to select any of the installed JVM’s but it makes the .html file much more complex • For an example see Core Java 2 page 37 • Fortunately, the simple .html file delimited between the the APPLET and /APPLET tags is sufficient for more recent browsers compatible with Java 2 (such as Netscape 6 or Netscape 7) <APPLET CODE = "HelloWorldApplet.class" WIDTH = 300 HEIGHT = 300 > </APPLET>
Also, a .htmlconverter can automatically generate the code for the plug-in from this simple .html file • The appletviewer program can understand the basic .html file and is a good tool for testing applets outside of the browser • Can be called from the Textpad editor
Applet attributes • These are specified after the APPLET tag which tell the browser how to load and display the applet • The general form is as follows (attributes in [..] are optional) < APPLET [CODEBASE =codebaseURL] CODE =appletFile [ARCHIVE = jarFile] [ALT =alternateText] [NAME =appletInstanceName] WIDTH =pixelsHEIGHT =pixels [ALIGN =alignment] [VSPACE =pixels] [HSPACE =pixels] </APPLET>
We will only look at the most common attributes • WIDTH, HEIGHT • The width and height of the applet window in pixels • Note that in a browser, the applet cannot be resized so you need to make a good guess at these values
The CODE attribute is simply the name of the applet class file • The CODEBASE attribute to tells the browser in which directory the applet's files are located • If this attribute is not specified, the .class file is in the same directory as the .html file • If it is specified, it is the relative path from the directory of the .html file
“…/myDirectory/myHTML.html” <APPLET CODE=myApplet.class CODEBASE=“appletDir/" WIDTH=500 HEIGHT=500> </APPLET> /myDirectory /appletDir myHTML.html myApplet.class
NAME • Used when applets on the same page want to communicate and calling applets from JavaScript • ARCHIVE • Used for more efficiently downloading multiple .class files in one Java archive file (see later)
Passing parameters to applets • Applets can use parameters passed to them from the .html file • This is done using the PARAM tag in the .html file • The NAME and VALUE tags then specify the parameter name and value <APPLET CODE=AppletClass.class WIDTH=500 HEIGHT=500> <PARAM NAME=parameter1Name VALUE=aValue> <PARAM NAME=parameter2NameVALUE=anotherValue> </APPLET>
The getParameter() method of JApplet then retrieves that (string) parameter • Parameters allow the applet versatility to be increased without the need to recompile the applet code • The following example is the ‘Hello World’ applet with the printed message passed as a parameter • http://www.eee.bham.ac.uk/spannm/Java%20Stuff/MessageApplet/MessageApplet.html
<APPLET CODE = "MessageApplet.class" WIDTH = 500 HEIGHT = 300 > <PARAM NAME=message VALUE="Another Message"> </APPLET> • The html file is as follows • Note that parameters are strings but they can be converted to other types (eg integers) using string parsing String messageValue=getParameter("message"); • The parameter is retrieved using the following line in the Java program
class MessagePanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Font f=new Font("SansSerif",Font.BOLD,36); g.setFont(f); g.drawString(messageValue, 50, 120); } public MessagePanel(String m) { messageValue=m; } private String messageValue; } public class MessageApplet extends JApplet { public void init() { Container contentPane = getContentPane(); String messageValue=getParameter("message"); contentPane.add(new MessagePanel(messageValue)); } }
Applet security • Applets are programs downloaded from a remote site and run automatically on the users home computer • They constitute potentially a huge security risk! • For this reason, applets are extremely limited in their functionality • Attempts to perform other functions leads to a SecurityException being generated
Applets cannot • Read or write to the local computer’s file system • If this were not the case, applets could potentially read sensitive data (credit card info!) or delete important files • Applets cannot find information about the local computer. For example user names and email addresses • Applets cannot run a local executable program (for obvious reasons!)
Applets cannot • Communicate with any host except its originating host • Applets can only phone home! • If this were not the case, applets could access web pages behind corporate firewalls • They could then read potentially sensitive company information
Corporate intranet Executing applet applet Corporate web server Applet originating host Firewall
JAR files • The previous MessageApplet example actually creates 2 class files • MessageApplet.class • MessagePanel.class • The browser needs to load both classes to run the applet • The browser must make 2 connections to the server • Typically an applet consists of many .class files
JAR files enable the .class filesfor an applet to be downloaded using a single connection to the server • The class files are loaded into a JAR file using the following command jar cf MessageApplet.jar MessageApplet.class MessagePanel.class • The MessageApplet.jar JAR file then contains the two class files
The JAR file is then referenced in the APPLET tagof the html file using the ARCHIVE tag <APPLET CODE = "MessageApplet.class" ARCHIVE=“MessageApplet.jar” WIDTH = 500 HEIGHT = 300 > <PARAM NAME=message VALUE="Another Message"> </APPLET> • JAR files are extensively used in large Java projects as repositories of classes which can be included in the CLASSSPATH
And finally….. • Applets are very useful but are basically Java programs with a bit of extra ‘wrapping’ • Great for me as I have used them extensively in this course to demonstrate Java programs as I can run them from Powerpoint • Their limited functionality makes them difficult to write if we want to do ‘real’ work