1 / 34

EE2E1. JAVA Programming

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.

mmcduffie
Download Presentation

EE2E1. JAVA Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EE2E1. JAVA Programming Lecture 10 Applets

  2. 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

  3. 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

  4. 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

  5. .html web page MyApplet.class Client running Netscape Web server

  6. 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

  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) <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

  9. 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

  10. 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()

  11. 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_index/d.html • A favourite of mine is http://javaboutique.internet.com/Date2Day/

  13. 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

  14. Client computer Browser Java virtual machine Java virtual machine html file <… code for plug-in …>

  15. 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>

  16. 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

  17. 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>

  18. 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 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” <APPLET CODE=myApplet.class CODEBASE=“appletDir/" WIDTH=500 HEIGHT=500> </APPLET> /myDirectory /appletDir myHTML.html myApplet.class

  21. 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)

  22. 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>

  23. 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

  24. <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

  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 • 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 • 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 • 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. Corporate intranet Executing applet applet Corporate web server Applet originating host Firewall

  30. 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

  31. 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

  32. 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

  33. 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

More Related