Download presentation
Presentation is loading. Please wait.
1
EE2E1. JAVA Programming Lecture 10 Applets
2
Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello World!” applet A simple example “Hello World!” applet Converting an application to an applet Converting an application to an applet Browsers and plug-ins Browsers and plug-ins Applet attributes Applet attributes Passing parameters to applets Passing parameters to applets Applet security Applet security JAR files JAR files
3
Introduction Java applets are programs written in Java which are run by a web browser such as Netscape or Internet Explorer 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 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 Java applets are not the be all and end all of Java programming but they can be useful
4
Web-servers and clients When we run a web browser to access the internet, the web browser program runs on a client computer 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 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
5
Client running Netscape Web server.html web page MyApplet.class
6
Example – A “Hello World!” applet The following example applet draws a “Hello World” string The following example applet draws a “Hello World” string http://www.eee.bham.ac.uk/spannm/Java%20St uff/HelloWorldApplet/HelloWorldApplet.html http://www.eee.bham.ac.uk/spannm/Java%20St uff/HelloWorldApplet/HelloWorldApplet.html http://www.eee.bham.ac.uk/spannm/Java%20St uff/HelloWorldApplet/HelloWorldApplet.html It is essentially a very simple Swing program However, there is no main 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 We also need a.html file to tell the browser to download and run the applet
7
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()); }
8
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) 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 > This simple.html file is fine if we don’t need a Java plug-in (see) later with our browser 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
9
Converting an application to an applet An applet is essentially a graphics container (rather like the JFrame) class 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
10
An applet has no main() method 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 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()
11
Other methods of JApplet that you may need to implement 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
12
Lots of other example applets can be found at http://javaboutique.internet.com/applet_inde x/d.html Lots of other example applets can be found at http://javaboutique.internet.com/applet_inde x/d.html http://javaboutique.internet.com/applet_inde x/d.html http://javaboutique.internet.com/applet_inde x/d.html A favourite of mine is http://javaboutique.internet.com/Date2Da y/ http://javaboutique.internet.com/Date2Da y/ http://javaboutique.internet.com/Date2Da y/
13
Browsers and plug-ins Internet browsers such as Explorer and Netscape have not kept up with the development of Java 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 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
14
Java virtual machine Browser Java virtual machine html file Client computer
15
Plug-ins allow the user to select any of the installed JVM’s but it makes the.html file much more complex 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) 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 >
16
Also, a.html converter can automatically generate the code for the plug-in from this simple.html file Also, a.html converter 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 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
17
Applet attributes These are specified after the APPLET tag which tell the browser how to load and display the applet 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 = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels] [HSPACE = pixels]
18
We will only look at the most common attributes 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
19
The CODE attribute is simply the name of the applet class file 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 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
20
/myDirectory myHTML.html /appletDir myApplet.class <APPLET CODE=myApplet.class CODEBASE=“appletDir/" WIDTH=500 HEIGHT=500> “…/myDirectory/myHTML.html”
21
NAME NAME Used when applets on the same page want to communicate and calling applets from JavaScript ARCHIVE ARCHIVE Used for more efficiently downloading multiple.class files in one Java archive file (see later)
22
Passing parameters to applets Applets can use parameters passed to them from the.html file 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>
23
The getParameter() method of JApplet then retrieves that (string) parameter 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 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 The following example is the ‘Hello World’ applet with the printed message passed as a parameter http://www.eee.bham.ac.uk/spannm/Java%20St uff/MessageApplet/MessageApplet.html http://www.eee.bham.ac.uk/spannm/Java%20St uff/MessageApplet/MessageApplet.html http://www.eee.bham.ac.uk/spannm/Java%20St uff/MessageApplet/MessageApplet.html
24
The html file is as follows The html file is as follows String messageValue=getParameter("message"); The parameter is retrieved using the following line in the Java program The parameter is retrieved using the following line in the Java program Note that parameters are strings but they can be converted to other types (eg integers) using string parsing Note that parameters are strings but they can be converted to other types (eg integers) using string parsing
25
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)); }
26
Applet security Applets are programs downloaded from a remote site and run automatically on the users home computer 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
27
Applets cannot 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!)
28
Applets cannot 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
29
Applet originating host applet Corporate intranet Firewall Executing applet Corporate web server
30
JAR files The previous MessageApplet example actually creates 2 class 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 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
31
JAR files enable the.class files for an applet to be downloaded using a single connection to the server JAR files enable the.class files for 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
32
The JAR file is then referenced in the APPLET tag of the html file using the ARCHIVE tag The JAR file is then referenced in the APPLET tag of the html file using the ARCHIVE tag <APPLET CODE = "MessageApplet.class" ARCHIVE=“MessageApplet.jar” WIDTH = 500 HEIGHT = 300 > JAR files are extensively used in large Java projects as repositories of classes which can be included in the CLASSSPATH JAR files are extensively used in large Java projects as repositories of classes which can be included in the CLASSSPATH
33
And finally….. Applets are very useful but are basically Java programs with a bit of extra ‘wrapping’ 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 Their limited functionality makes them difficult to write if we want to do ‘real’ work
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.