ee2e1. java programming lecture 10 applets. contents introduction introduction web-servers and...

34
EE2E1. JAVA Programming Lecture 10 Lecture 10 Applets Applets

Post on 21-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

EE2E1. JAVA Programming

Lecture 10Lecture 10

AppletsApplets

Page 2: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Contents IntroductionIntroduction Web-servers and clientsWeb-servers and clients A simple example “Hello World!” applet A simple example “Hello World!” applet Converting an application to an appletConverting an application to an applet Browsers and plug-insBrowsers and plug-ins Applet attributes Applet attributes Passing parameters to appletsPassing parameters to applets Applet securityApplet security JAR filesJAR files

Page 3: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Introduction Java applets are programs written in Java which are run by Java applets are programs written in Java which are run by

a web browser such as a web browser such as Netscape Netscape or or Internet ExplorerInternet Explorer The applet code is downloaded from its host computer The applet code is downloaded from its host computer

each time it is runeach time it is run Applets are used for adding extra functionality to web Applets are used for adding extra functionality to web

pagespages As we shall see, converting a normal Java application As we shall see, converting a normal Java application

program to an applet is easyprogram to an applet is easy The main problems stem from the browser The main problems stem from the browser

incompatibility and security issuesincompatibility and security issues Java applets are not the Java applets are not the be all and end allbe all and end all of Java of Java

programming but they can be usefulprogramming but they can be useful

Page 4: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Web-servers and clients

When we run a web browser to access the internet, When we run a web browser to access the internet, the web browser program runs on a the web browser program runs on a clientclient computercomputer Usually the PC from which we are workingUsually the PC from which we are working

The The .html.html web page we are accessing is on the web page we are accessing is on the Web Server Web Server computercomputer It is on this machine that the code (the It is on this machine that the code (the

Java Java .class.class file) for the Java applet resides file) for the Java applet resides which the .which the .htmlhtml file imports file imports

Page 5: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Client running Netscape

Web server

.html web page

MyApplet.class

Page 6: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Example – A “Hello World!” applet

The following example applet draws a “Hello The following example applet draws a “Hello World” stringWorld” string http://www.http://www.eeeeee..bhambham.ac..ac.ukuk//spannmspannm

/Java%20Stuff//Java%20Stuff/HelloWorldAppletHelloWorldApplet//HelloWorldAppletHelloWorldApplet.html.html

It is essentially a very simple It is essentially a very simple SwingSwing program program However, there is no However, there is no mainmain program program

Instead there is an Instead there is an init()init() method within a sub-class method within a sub-class of of JAppletJApplet

We also need a We also need a .html.html file to tell the browser to file to tell the browser to download and run the appletdownload and run the applet

Page 7: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

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()); }}

Page 8: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

The only other thing required is a The only other thing required is a .html.html which tells which tells the browser to load a particular Java the browser to load a particular Java class class file file containing the applet code (plus the size of the containing the applet code (plus the size of the window in which to run the applet)window in which to run the applet)

<APPLET CODE = "HelloWorldApplet.class" WIDTH = 300 HEIGHT = 300 >

</APPLET>

This simple .This simple .html html file is fine if we don’t need a file is fine if we don’t need a Java plug-in (see) later with our browserJava plug-in (see) later with our browser

OK for Netscape 6.2 and later versionsOK for Netscape 6.2 and later versions .html.html file more cumbersome for use with file more cumbersome for use with

earlier versions of Netscapeearlier versions of Netscape

Page 9: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Converting an application to an applet An applet is essentially a graphics container An applet is essentially a graphics container

(rather like the (rather like the JFrame)JFrame) class class

The The JAppletJApplet class replaces class replaces JFrame JFrame as the as the outer containerouter container

public class HelloWorldApplet extends JApplet

JAppletJApplet is a sub-class of is a sub-class of Container Container as is as is JFrame JFrame and contains many of and contains many of the same methodsthe same methods

Page 10: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

An applet has no An applet has no main()main() method method

This is replaced by the This is replaced by the init()init() method and is method and is called by the browser to perform initializationscalled by the browser to perform initializations

Usually an applet has no constructorUsually an applet has no constructor

The browser determines the graphics container The browser determines the graphics container title and the size is determined in the SIZE title and the size is determined in the SIZE parameters of the .parameters of the .html html filefile No need for calls to No need for calls to setSize()setSize() and and setTitle()setTitle()

Page 11: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Other methods ofOther methods of JApplet JApplet that you may need to that you may need to implementimplement start()start()

Automatically called by the browser after Automatically called by the browser after init() init() and when the user returns to the web page and when the user returns to the web page (whereas (whereas init()init() only called once) only called once)

stop()stop()Automatically called when the user moves off Automatically called when the user moves off

the web-page. the web-page. destroy()destroy()

Automatically called when the browser shuts Automatically called when the browser shuts down (after it calls the down (after it calls the stop()stop() method) method)

Normally put code to reclaim resources hereNormally put code to reclaim resources here

Page 12: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Lots of other example applets can be found Lots of other example applets can be found at at http://http://javaboutiquejavaboutique.internet.com/applet_index/d.html.internet.com/applet_index/d.html A favourite of mine is A favourite of mine is http://http://javaboutiquejavaboutique

.internet.com/Date2Day/.internet.com/Date2Day/

Page 13: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Browsers and plug-ins Internet browsers such as Explorer and Netscape have not Internet browsers such as Explorer and Netscape have not

kept up with the development of Javakept up with the development of Java Both contain a Java Virtual Machine (JVM) but earlier Both contain a Java Virtual Machine (JVM) but earlier

versions of the browsers can only run Java 1.0 programs versions of the browsers can only run Java 1.0 programs not the more recent Java 2not the more recent Java 2

This is always going to be a problem as Java is developing This is always going to be a problem as Java is developing much faster than internet browsersmuch faster than internet browsers

To combat his problem, Sun developed a Java To combat his problem, Sun developed a Java plug-inplug-in which which ‘plugs into’ either IE or Netscape‘plugs into’ either IE or Netscape Allows the browser to run the JVM installed on the Allows the browser to run the JVM installed on the

machine running the browser and not its internal versionmachine running the browser and not its internal version

Page 14: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Java virtual machine

Browser

Java virtual machine

html file

<… code for plug-in …>

Client computer

Page 15: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Plug-ins allow the user to select any of the Plug-ins allow the user to select any of the installed JVM’s but it makes the .installed JVM’s but it makes the .htmlhtml file file much more complexmuch more complex For an example see For an example see Core Java 2Core Java 2 page 37 page 37

Fortunately, the simple .Fortunately, the simple .htmlhtml file delimited file delimited between the the between the the APPLETAPPLET and /APPLET and /APPLET tags is sufficient for more recent browsers tags is sufficient for more recent browsers compatible with Java 2 (such as Netscape 6 compatible with Java 2 (such as Netscape 6 or Netscape 7)or Netscape 7)

<APPLET CODE = "HelloWorldApplet.class" WIDTH = 300 HEIGHT = 300 >

</APPLET>

Page 16: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Also, a .Also, a .htmlhtml converterconverter can automatically can automatically generate the code for the plug-in from this generate the code for the plug-in from this simple .simple .htmlhtml file file

The The appletviewer appletviewer program can understand program can understand the basic .the basic .htmlhtml file and is a good tool for file and is a good tool for testing applets outside of the browsertesting applets outside of the browser Can be called from the Can be called from the TextpadTextpad editor editor

Page 17: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Applet attributes These are specified after the APPLET tag which These are specified after the APPLET tag which

tell the browser how to load and display the applettell the browser how to load and display the applet The general form is as follows (attributes in [..] The general form is as follows (attributes in [..]

are optional)are optional)

< APPLET [CODEBASE = codebaseURL] CODE = appletFile[ARCHIVE = jarFile][ALT = alternateText] [NAME = appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels] [HSPACE = pixels]

</APPLET>

Page 18: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

We will only look at the most common We will only look at the most common attributesattributes WIDTH, HEIGHTWIDTH, HEIGHT

The width and height of the applet The width and height of the applet window in pixelswindow in pixels

Note that in a browser, the applet Note that in a browser, the applet cannotcannot be resized so you need to make be resized so you need to make a good guess at these valuesa good guess at these values

Page 19: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

The CODE attribute is simply the name of the The CODE attribute is simply the name of the applet applet class class filefile

TThe CODEBASE attribute to tellhe CODEBASE attribute to tellss the browser in the browser in which directory the applet's files are located which directory the applet's files are located If this attribute is not specified, the .If this attribute is not specified, the .classclass file is file is

in the same directory as the .in the same directory as the .html html filefile If it is specified, it is the relative path from the If it is specified, it is the relative path from the

directory of the .directory of the .htmlhtml file file

Page 20: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

/myDirectory

myHTML.html /appletDir

myApplet.class

<APPLET CODE=myApplet.class CODEBASE=“appletDir/" WIDTH=500 HEIGHT=500> </APPLET>

“…/myDirectory/myHTML.html”

Page 21: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

NAMENAME Used when applets on the same page Used when applets on the same page

want to communicate and calling applets want to communicate and calling applets from JavaScriptfrom JavaScript

ARCHIVEARCHIVE Used for more efficiently downloading Used for more efficiently downloading

multiple .multiple .class class files in one Java archive files in one Java archive file (see later)file (see later)

Page 22: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Passing parameters to applets Applets can use parameters passed to them from the Applets can use parameters passed to them from the

..htmlhtml file file This is done using the This is done using the PARAMPARAM tag in the . tag in the .htmlhtml

filefile The The NAMENAME and and VALUEVALUE tags then specify the tags then specify the

parameter name and valueparameter name and value

<APPLET CODE=Applet<APPLET CODE=AppletCClass.class lass.class WIDTH=WIDTH=500 500 HEIGHT= HEIGHT=500500> > <PARAM NAME=parameter1Name VALUE=aValue> <PARAM NAME=parameter1Name VALUE=aValue> <PARAM NAME=parameter2NameVALUE=anotherValue> <PARAM NAME=parameter2NameVALUE=anotherValue> </APPLET> </APPLET>

Page 23: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

The The getParameter()getParameter() method of method of JAppletJApplet then then retrieves that (string) parameterretrieves that (string) parameter

Parameters allow the applet versatility to be Parameters allow the applet versatility to be increased without the need to recompile the applet increased without the need to recompile the applet codecode

The following example is the ‘Hello World’ applet The following example is the ‘Hello World’ applet with the printed message passed as a parameterwith the printed message passed as a parameter

http://www.http://www.eeeeee..bhambham.ac..ac.ukuk//spannmspannm/Java%20Stuff//Java%20Stuff/MessageAppletMessageApplet//MessageAppletMessageApplet.html.html

Page 24: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

The The htmlhtml file is as follows file is as follows

<APPLET CODE = "MessageApplet.class" WIDTH = 500 HEIGHT = 300 ><PARAM NAME=message VALUE="Another Message"></APPLET>

String messageValue=getParameter("message");

The parameter is retrieved using the following The parameter is retrieved using the following line in the Java programline in the Java program

Note that parameters are strings but they can be Note that parameters are strings but they can be converted to other types (eg integers) using converted to other types (eg integers) using string parsingstring parsing

Page 25: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

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

Page 26: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Applet security

Applets are programs downloaded from a Applets are programs downloaded from a remote site and run remote site and run automaticallyautomatically on the on the users home computerusers home computer They constitute potentially a huge They constitute potentially a huge

security risk!security risk! For this reason, applets are extremely For this reason, applets are extremely

limited in their functionalitylimited in their functionality Attempts to perform other functions leads Attempts to perform other functions leads

to a to a SecurityExceptionSecurityException being generated being generated

Page 27: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Applets Applets cannotcannot Read or write to the local computer’s file Read or write to the local computer’s file

systemsystemIf this were not the case, applets could If this were not the case, applets could

potentially read sensitive data (credit potentially read sensitive data (credit card info!) or delete important filescard info!) or delete important files

Applets cannot find information about Applets cannot find information about the local computer. For example user the local computer. For example user names and email addressesnames and email addresses

Applets cannot run a local executable Applets cannot run a local executable program (for obvious reasons!)program (for obvious reasons!)

Page 28: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Applets cannotApplets cannot Communicate with any host except its Communicate with any host except its

originatingoriginating host hostApplets can only phone home!Applets can only phone home!If this were not the case, applets could If this were not the case, applets could

access web pages behind corporate access web pages behind corporate firewallsfirewalls

They could then read potentially They could then read potentially sensitive company informationsensitive company information

Page 29: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

Applet originating host

applet

Corporate intranet

Firewall

Executing applet

Corporate web server

Page 30: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

JAR files

The previous The previous MessageAppletMessageApplet example actually example actually creates 2 creates 2 class class filesfiles MessageApplet.classMessageApplet.class MessagePanel.classMessagePanel.class

The browser needs to load both classes to run the The browser needs to load both classes to run the appletapplet The browser must make 2 connections to the The browser must make 2 connections to the

serverserver Typically an applet consists of many .Typically an applet consists of many .classclass files files

Page 31: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

JAR files enable the .JAR files enable the .class class filesfiles for an for an applet to be downloaded using a single applet to be downloaded using a single connection to the serverconnection to the server The class files are loaded into a JAR file The class files are loaded into a JAR file

using the following commandusing the following command

jar cf MessageApplet.jar MessageApplet.class MessagePanel.class

The The MessageApplet.jarMessageApplet.jar JAR file then JAR file then contains the two contains the two class filesclass files

Page 32: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

The JAR file is then referenced in the APPLET tagThe JAR file is then referenced in the APPLET tag of the of the htmlhtml file using the ARCHIVE tag 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 JAR files are extensively used in large Java projects as repositories of classes which can be projects as repositories of classes which can be included in the CLASSSPATHincluded in the CLASSSPATH

Page 33: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello

And finally…..

Applets are very useful but are basically Java Applets are very useful but are basically Java programs with a bit of extra ‘wrapping’programs with a bit of extra ‘wrapping’ Great for me as I have used them extensively Great for me as I have used them extensively

in this course to demonstrate Java programs in this course to demonstrate Java programs as I can run them from Powerpointas I can run them from Powerpoint

Their limited functionality makes them Their limited functionality makes them difficult to write if we want to do ‘real’ workdifficult to write if we want to do ‘real’ work

Page 34: EE2E1. JAVA Programming Lecture 10 Applets. Contents Introduction Introduction Web-servers and clients Web-servers and clients A simple example “Hello