Applets as front-ends to server-side programming DD1335 (Lecture 7) - - PowerPoint PPT Presentation

applets as front ends to server side programming
SMART_READER_LITE
LIVE PREVIEW

Applets as front-ends to server-side programming DD1335 (Lecture 7) - - PowerPoint PPT Presentation

Applets as front-ends Applets as front-ends to server-side programming DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18 Applets as front-ends Applets as front-ends to server-side programming Objectives DD1335 (Lecture 7)


slide-1
SLIDE 1

Applets as front-ends

Applets as front-ends to server-side programming

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18

slide-2
SLIDE 2

Applets as front-ends

Applets as front-ends to server-side programming

Objectives

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18

slide-3
SLIDE 3

Applets as front-ends

Applets as front-ends to server-side programming

Objectives

◮ Introduce applets

◮ Examples of Java graphical programming ◮ How-to put an applet in a HTML page ◮ The HTML Applet tag and alternatives DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18

slide-4
SLIDE 4

Applets as front-ends

Applets as front-ends to server-side programming

Objectives

◮ Introduce applets

◮ Examples of Java graphical programming ◮ How-to put an applet in a HTML page ◮ The HTML Applet tag and alternatives

◮ Applet communication with the environment

◮ Applet-Browser (AppletContext) ◮ Applet-Applet ◮ Applet-JavaScript and JavaScript-Applet ◮ Applet-page using DOM DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18

slide-5
SLIDE 5

Applets as front-ends

Applets as front-ends to server-side programming

Objectives

◮ Introduce applets

◮ Examples of Java graphical programming ◮ How-to put an applet in a HTML page ◮ The HTML Applet tag and alternatives

◮ Applet communication with the environment

◮ Applet-Browser (AppletContext) ◮ Applet-Applet ◮ Applet-JavaScript and JavaScript-Applet ◮ Applet-page using DOM

◮ Applet signing

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18

slide-6
SLIDE 6

Applets as front-ends

Applets as front-ends to server-side programming

Objectives

◮ Introduce applets

◮ Examples of Java graphical programming ◮ How-to put an applet in a HTML page ◮ The HTML Applet tag and alternatives

◮ Applet communication with the environment

◮ Applet-Browser (AppletContext) ◮ Applet-Applet ◮ Applet-JavaScript and JavaScript-Applet ◮ Applet-page using DOM

◮ Applet signing ◮ Applet-server communication

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18

slide-7
SLIDE 7

Applets as front-ends

Applets as front-ends to server-side programming

Objectives

◮ Introduce applets

◮ Examples of Java graphical programming ◮ How-to put an applet in a HTML page ◮ The HTML Applet tag and alternatives

◮ Applet communication with the environment

◮ Applet-Browser (AppletContext) ◮ Applet-Applet ◮ Applet-JavaScript and JavaScript-Applet ◮ Applet-page using DOM

◮ Applet signing ◮ Applet-server communication ◮ Media in Applets and in Java

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 1 / 18

slide-8
SLIDE 8

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-9
SLIDE 9

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-10
SLIDE 10

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-11
SLIDE 11

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

◮ However, there are security restrictions on applets downloaded from other

sites than the applet’s site

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-12
SLIDE 12

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

◮ However, there are security restrictions on applets downloaded from other

sites than the applet’s site

◮ E.g. the local file system cannot be freely accessed DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-13
SLIDE 13

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

◮ However, there are security restrictions on applets downloaded from other

sites than the applet’s site

◮ E.g. the local file system cannot be freely accessed

◮ Applets are written as subclasses of java.applet.Applet

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-14
SLIDE 14

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

◮ However, there are security restrictions on applets downloaded from other

sites than the applet’s site

◮ E.g. the local file system cannot be freely accessed

◮ Applets are written as subclasses of java.applet.Applet

◮ They redefine some methods to achieve desired functionality DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-15
SLIDE 15

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

◮ However, there are security restrictions on applets downloaded from other

sites than the applet’s site

◮ E.g. the local file system cannot be freely accessed

◮ Applets are written as subclasses of java.applet.Applet

◮ They redefine some methods to achieve desired functionality

◮ Since applets are downloaded before execution, large code may make the user

wait quite a lot

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-16
SLIDE 16

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

◮ However, there are security restrictions on applets downloaded from other

sites than the applet’s site

◮ E.g. the local file system cannot be freely accessed

◮ Applets are written as subclasses of java.applet.Applet

◮ They redefine some methods to achieve desired functionality

◮ Since applets are downloaded before execution, large code may make the user

wait quite a lot

◮ Caching in browser cache was the initial mechanism DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-17
SLIDE 17

Applets as front-ends

Applets

◮ Applets are based on a Java Virtual Machine running inside a browser as a

Plug-in

◮ As graphical applications, applets can give more interactive interfaces than e.g.

HTML forms

◮ Since they have all the Java functionality, applets can connect to a server and

communicate with it just like any Java app.

◮ However, there are security restrictions on applets downloaded from other

sites than the applet’s site

◮ E.g. the local file system cannot be freely accessed

◮ Applets are written as subclasses of java.applet.Applet

◮ They redefine some methods to achieve desired functionality

◮ Since applets are downloaded before execution, large code may make the user

wait quite a lot

◮ Caching in browser cache was the initial mechanism ◮ Now the Java plugin has more sophisticated caching features DD1335 (Lecture 7) Basic Internet Programming Spring 2010 2 / 18

slide-18
SLIDE 18

Applets as front-ends

Example - java code

import java.applet.*; import java.awt.*; // needed for Graphics public class FirstApplet extends Applet { // we draw a Hello. No interaction public void paint(Graphics g) { g.drawString("Hello!", 25, 50); } }

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 3 / 18

slide-19
SLIDE 19

Applets as front-ends

Example - HTML code

<html> <head> <title> My first applet </title> </head> <body> <applet code="FirstApplet.class" width="150" height="50" /> </body> </html>

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 4 / 18

slide-20
SLIDE 20

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-21
SLIDE 21

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-22
SLIDE 22

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-23
SLIDE 23

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

◮ Test with

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-24
SLIDE 24

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

◮ Test with

◮ appletviewer file.html or DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-25
SLIDE 25

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

◮ Test with

◮ appletviewer file.html or ◮ (most often) load the HTML in a www-browser. DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-26
SLIDE 26

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

◮ Test with

◮ appletviewer file.html or ◮ (most often) load the HTML in a www-browser. ◮ Normally, browsers have a ”Java Console” where you can see exceptions,

System.out output et.c.

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-27
SLIDE 27

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

◮ Test with

◮ appletviewer file.html or ◮ (most often) load the HTML in a www-browser. ◮ Normally, browsers have a ”Java Console” where you can see exceptions,

System.out output et.c.

◮ To reload the applet class after a change, reloading the page may not be

enough!

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-28
SLIDE 28

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

◮ Test with

◮ appletviewer file.html or ◮ (most often) load the HTML in a www-browser. ◮ Normally, browsers have a ”Java Console” where you can see exceptions,

System.out output et.c.

◮ To reload the applet class after a change, reloading the page may not be

enough!

◮ Shift-reload may work. Ctrl-Shift-R, or Ctrl-Shift-F5 DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-29
SLIDE 29

Applets as front-ends

Applet How-To

◮ Make a subclass of java.applet.Applet and compile

◮ To use the latest GUI libraries, use javax.swing.JApplet

◮ Make a HTML file that refers to the applet via the APPLET tag and its CODE

attribute

◮ Test with

◮ appletviewer file.html or ◮ (most often) load the HTML in a www-browser. ◮ Normally, browsers have a ”Java Console” where you can see exceptions,

System.out output et.c.

◮ To reload the applet class after a change, reloading the page may not be

enough!

◮ Shift-reload may work. Ctrl-Shift-R, or Ctrl-Shift-F5 ◮ In the Java Plugin console, press x to clean class cache (press h for other

commands)

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 5 / 18

slide-30
SLIDE 30

Applets as front-ends

A simple graphical applet

import java.applet.*; import java.awt.event.*; import java.awt.*; public class SimpleGraphicalApplet extends Applet implements java.awt.event.ActionListener { TextField input= new TextField(); TextArea output= new TextArea(3, 20); /* constructor: arrange the two buttons nicely */ public SimpleGraphicalApplet(){

  • utput.setEditable(false); //no input!

setLayout(new java.awt.BorderLayout()); add(input, "North"); add(output, "Center"); input.addActionListener(this); } public void actionPerformed(ActionEvent ae){

  • utput.setText(input.getText());

input.setText(""); } }

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 6 / 18

slide-31
SLIDE 31

Applets as front-ends

A simple graphical applet

import java.applet.*; import java.awt.event.*; import java.awt.*; public class SimpleGraphicalApplet extends Applet implements java.awt.event.ActionListener { TextField input= new TextField(); TextArea output= new TextArea(3, 20); /* constructor: arrange the two buttons nicely */ public SimpleGraphicalApplet(){

  • utput.setEditable(false); //no input!

setLayout(new java.awt.BorderLayout()); add(input, "North"); add(output, "Center"); input.addActionListener(this); } public void actionPerformed(ActionEvent ae){

  • utput.setText(input.getText());

input.setText(""); } } <applet code="SimpleGraphicalApplet" width="200" height="300"> </applet>

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 6 / 18

slide-32
SLIDE 32

Applets as front-ends

Applet lifecycle

◮ As in the previous example, the applet constructor is a good place to define the

graphical layout and interaction

◮ Once the applet is constructed, it will react to user input DD1335 (Lecture 7) Basic Internet Programming Spring 2010 7 / 18

slide-33
SLIDE 33

Applets as front-ends

Applet lifecycle

◮ As in the previous example, the applet constructor is a good place to define the

graphical layout and interaction

◮ Once the applet is constructed, it will react to user input ◮ So nothing like a main() or service() method are needed unless you

want to be able to start your application either as an applet or as a stand alone application

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 7 / 18

slide-34
SLIDE 34

Applets as front-ends

Applet lifecycle

◮ As in the previous example, the applet constructor is a good place to define the

graphical layout and interaction

◮ Once the applet is constructed, it will react to user input ◮ So nothing like a main() or service() method are needed unless you

want to be able to start your application either as an applet or as a stand alone application

◮ java.applet.Applet also defines a number of methods to treat interaction

with the browser

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 7 / 18

slide-35
SLIDE 35

Applets as front-ends

Applet lifecycle

◮ As in the previous example, the applet constructor is a good place to define the

graphical layout and interaction

◮ Once the applet is constructed, it will react to user input ◮ So nothing like a main() or service() method are needed unless you

want to be able to start your application either as an applet or as a stand alone application

◮ java.applet.Applet also defines a number of methods to treat interaction

with the browser

◮ init() ◮ Is called after the browser has downloaded the applet DD1335 (Lecture 7) Basic Internet Programming Spring 2010 7 / 18

slide-36
SLIDE 36

Applets as front-ends

Applet lifecycle

◮ As in the previous example, the applet constructor is a good place to define the

graphical layout and interaction

◮ Once the applet is constructed, it will react to user input ◮ So nothing like a main() or service() method are needed unless you

want to be able to start your application either as an applet or as a stand alone application

◮ java.applet.Applet also defines a number of methods to treat interaction

with the browser

◮ init() ◮ Is called after the browser has downloaded the applet ◮ start() ◮ Called after init() and every time the user comes back to the applet page (if

the applet hasn’t been destroyed)

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 7 / 18

slide-37
SLIDE 37

Applets as front-ends

Applet lifecycle

◮ As in the previous example, the applet constructor is a good place to define the

graphical layout and interaction

◮ Once the applet is constructed, it will react to user input ◮ So nothing like a main() or service() method are needed unless you

want to be able to start your application either as an applet or as a stand alone application

◮ java.applet.Applet also defines a number of methods to treat interaction

with the browser

◮ init() ◮ Is called after the browser has downloaded the applet ◮ start() ◮ Called after init() and every time the user comes back to the applet page (if

the applet hasn’t been destroyed)

◮ stop() ◮ Called when the user leaves the applet page DD1335 (Lecture 7) Basic Internet Programming Spring 2010 7 / 18

slide-38
SLIDE 38

Applets as front-ends

Applet lifecycle

◮ As in the previous example, the applet constructor is a good place to define the

graphical layout and interaction

◮ Once the applet is constructed, it will react to user input ◮ So nothing like a main() or service() method are needed unless you

want to be able to start your application either as an applet or as a stand alone application

◮ java.applet.Applet also defines a number of methods to treat interaction

with the browser

◮ init() ◮ Is called after the browser has downloaded the applet ◮ start() ◮ Called after init() and every time the user comes back to the applet page (if

the applet hasn’t been destroyed)

◮ stop() ◮ Called when the user leaves the applet page ◮ destroy() ◮ Called when the browser exits, or the applet terminates, etc. ◮ It is entirely up to the browser when to call destroy() ◮ stop() is always called before destroy DD1335 (Lecture 7) Basic Internet Programming Spring 2010 7 / 18

slide-39
SLIDE 39

Applets as front-ends

import java.awt.*; import java.applet.*; public class TheLifeOfAnApplet extends Applet { public void init() { trace("init"); } public void start() { trace("start"); } public void stop() { trace("stop"); } public void destroy() { trace("destroy"); } public void paint(Graphics g){ trace(g, "paint"); } private void trace(String s) { System.out.println(s); trace(getGraphics(), s); //retrieve the graphical context } private void trace(Graphics g, String s) {trace(g, s, 50, 20);} private void trace(Graphics g, String s, int x, int y) { g.drawString("***", x, y); g.drawString(s, x, y + 30); g.drawString("***", x, y + 60); } }

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 8 / 18

slide-40
SLIDE 40

Applets as front-ends

The APPLET tag attributes

◮ CODEBASE

URL to the applet base if another than the current dir

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 9 / 18

slide-41
SLIDE 41

Applets as front-ends

The APPLET tag attributes

◮ CODEBASE

URL to the applet base if another than the current dir

◮ ALT

Text shown if the browser can’t show applet

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 9 / 18

slide-42
SLIDE 42

Applets as front-ends

The APPLET tag attributes

◮ CODEBASE

URL to the applet base if another than the current dir

◮ ALT

Text shown if the browser can’t show applet

◮ NAME

An applet name, used for communicating from other applets in the same page

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 9 / 18

slide-43
SLIDE 43

Applets as front-ends

The APPLET tag attributes

◮ CODEBASE

URL to the applet base if another than the current dir

◮ ALT

Text shown if the browser can’t show applet

◮ NAME

An applet name, used for communicating from other applets in the same page

◮ ALIGN, VSPACE, HSPACE, HEIGHT, WIDTH

Placing in the page

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 9 / 18

slide-44
SLIDE 44

Applets as front-ends

The APPLET tag attributes

◮ CODEBASE

URL to the applet base if another than the current dir

◮ ALT

Text shown if the browser can’t show applet

◮ NAME

An applet name, used for communicating from other applets in the same page

◮ ALIGN, VSPACE, HSPACE, HEIGHT, WIDTH

Placing in the page

◮ ARCHIVE

Comma-separated JAR files with applet code, resources, libraries needed, etc

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 9 / 18

slide-45
SLIDE 45

Applets as front-ends

The APPLET tag attributes

◮ CODEBASE

URL to the applet base if another than the current dir

◮ ALT

Text shown if the browser can’t show applet

◮ NAME

An applet name, used for communicating from other applets in the same page

◮ ALIGN, VSPACE, HSPACE, HEIGHT, WIDTH

Placing in the page

◮ ARCHIVE

Comma-separated JAR files with applet code, resources, libraries needed, etc

◮ OBJECT

Refers to an already-instantiated applet saved in a file on the server

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 9 / 18

slide-46
SLIDE 46

Applets as front-ends

APPLET tag alternatives

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 10 / 18

slide-47
SLIDE 47

Applets as front-ends

APPLET tag alternatives

◮ APPLET is deprecated in XHTML 1.0

It is still used and recognized by browsers

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 10 / 18

slide-48
SLIDE 48

Applets as front-ends

APPLET tag alternatives

◮ APPLET is deprecated in XHTML 1.0

It is still used and recognized by browsers

◮ OBJECT is used with Internet Explorer

It ensures that if Java is not installed in Explorer, the Java Plugin will be downloaded and installed at the first applet use OBJECT is also understood by Mozilla in XHTML, but attributes are different

https://eyeasme.com/Shayne/XHTML/appletObject.html

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 10 / 18

slide-49
SLIDE 49

Applets as front-ends

APPLET tag alternatives

◮ APPLET is deprecated in XHTML 1.0

It is still used and recognized by browsers

◮ OBJECT is used with Internet Explorer

It ensures that if Java is not installed in Explorer, the Java Plugin will be downloaded and installed at the first applet use OBJECT is also understood by Mozilla in XHTML, but attributes are different

https://eyeasme.com/Shayne/XHTML/appletObject.html

◮ EMBED is used in Mozilla

The situation does not appear to be very “standard” at the moment

http://java.sun.com/j2se/1.5.0/docs/guide/ ←

֓

plugin/developer_guide/using_tags.html

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 10 / 18

slide-50
SLIDE 50

Applets as front-ends

APPLET tag alternatives

◮ APPLET is deprecated in XHTML 1.0

It is still used and recognized by browsers

◮ OBJECT is used with Internet Explorer

It ensures that if Java is not installed in Explorer, the Java Plugin will be downloaded and installed at the first applet use OBJECT is also understood by Mozilla in XHTML, but attributes are different

https://eyeasme.com/Shayne/XHTML/appletObject.html

◮ EMBED is used in Mozilla

The situation does not appear to be very “standard” at the moment

http://java.sun.com/j2se/1.5.0/docs/guide/ ←

֓

plugin/developer_guide/using_tags.html

◮ In JSP you can look at the User-agent header to decide what kind of browser you

serve to JSP has a special action, <jsp:plugin>, which will generate correct code

http://java.sun.com/products/jsp/ ←

֓

syntax/2.0/syntaxref2023.html#1004158

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 10 / 18

slide-51
SLIDE 51

Applets as front-ends

Applet parameters

HTML:

<applet code="SomeApplet.class" width="500" height="320"> <param name="CourseName" value="Internet Programming" /> <param name="CourseID" value="2D1335" /> <param name="LectureNumber" value="7" /> </applet>

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 11 / 18

slide-52
SLIDE 52

Applets as front-ends

Applet parameters

HTML:

<applet code="SomeApplet.class" width="500" height="320"> <param name="CourseName" value="Internet Programming" /> <param name="CourseID" value="2D1335" /> <param name="LectureNumber" value="7" /> </applet>

Java:

String course = getParameter("CourseName"); if (course == null) course = "A KTH course"; String lectno = getParameter("LectureNumber "); int no = Integer.parseInt(lectno);

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 11 / 18

slide-53
SLIDE 53

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-54
SLIDE 54

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

◮ get another applet from the same page in order to call its methods:

getApplet(String name)

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-55
SLIDE 55

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

◮ get another applet from the same page in order to call its methods:

getApplet(String name)

◮ enumerate all other applets in the page: getApplets() DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-56
SLIDE 56

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

◮ get another applet from the same page in order to call its methods:

getApplet(String name)

◮ enumerate all other applets in the page: getApplets()

◮ Read audio clips and images from the net and give them to the applet:

getAudioClip(URL), getImage(URL)

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-57
SLIDE 57

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

◮ get another applet from the same page in order to call its methods:

getApplet(String name)

◮ enumerate all other applets in the page: getApplets()

◮ Read audio clips and images from the net and give them to the applet:

getAudioClip(URL), getImage(URL)

◮ Retrieve and show a URL in this browser frame or another:

showDocument(URL, String frame)

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-58
SLIDE 58

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

◮ get another applet from the same page in order to call its methods:

getApplet(String name)

◮ enumerate all other applets in the page: getApplets()

◮ Read audio clips and images from the net and give them to the applet:

getAudioClip(URL), getImage(URL)

◮ Retrieve and show a URL in this browser frame or another:

showDocument(URL, String frame)

◮ Retrieve and save information to communicate with applets from this page or

  • ther pages (“applet persistence”)

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-59
SLIDE 59

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

◮ get another applet from the same page in order to call its methods:

getApplet(String name)

◮ enumerate all other applets in the page: getApplets()

◮ Read audio clips and images from the net and give them to the applet:

getAudioClip(URL), getImage(URL)

◮ Retrieve and show a URL in this browser frame or another:

showDocument(URL, String frame)

◮ Retrieve and save information to communicate with applets from this page or

  • ther pages (“applet persistence”)

◮ void setStream(String key, InputStream stream) DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-60
SLIDE 60

Applets as front-ends

The applet context

◮ java.applet.AppletContext getAppletContext()

Represents basically the browser in which the applet runs. Capabilities:

◮ get another applet from the same page in order to call its methods:

getApplet(String name)

◮ enumerate all other applets in the page: getApplets()

◮ Read audio clips and images from the net and give them to the applet:

getAudioClip(URL), getImage(URL)

◮ Retrieve and show a URL in this browser frame or another:

showDocument(URL, String frame)

◮ Retrieve and save information to communicate with applets from this page or

  • ther pages (“applet persistence”)

◮ void setStream(String key, InputStream stream) ◮ InputStream getStream(String key) DD1335 (Lecture 7) Basic Internet Programming Spring 2010 12 / 18

slide-61
SLIDE 61

Applets as front-ends

Applet-Applet and JavaScript communication

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 13 / 18

slide-62
SLIDE 62

Applets as front-ends

Applet-Applet and JavaScript communication

◮ Adding the info of all applets in the page to a java.awt.TextArea called ’text’

Enumeration e = getAppletContext().getApplets(); while(e.hasMoreElements()) { text.append("\n" + ((Applet) e.nextElement()).getAppletInfo()); }

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 13 / 18

slide-63
SLIDE 63

Applets as front-ends

Applet-Applet and JavaScript communication

◮ Adding the info of all applets in the page to a java.awt.TextArea called ’text’

Enumeration e = getAppletContext().getApplets(); while(e.hasMoreElements()) { text.append("\n" + ((Applet) e.nextElement()).getAppletInfo()); }

◮ Calling a method of another applet defined as

<applet codebase="." code="examples.Applet3.class" name="Paint" width="400" height="300" />

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 13 / 18

slide-64
SLIDE 64

Applets as front-ends

Applet-Applet and JavaScript communication

◮ Adding the info of all applets in the page to a java.awt.TextArea called ’text’

Enumeration e = getAppletContext().getApplets(); while(e.hasMoreElements()) { text.append("\n" + ((Applet) e.nextElement()).getAppletInfo()); }

◮ Calling a method of another applet defined as

<applet codebase="." code="examples.Applet3.class" name="Paint" width="400" height="300" /> Applet other = getAppletContext().getApplet("Paint"); if(other != null) { ((Applet3) other).setInfo(new Date().toString()); }

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 13 / 18

slide-65
SLIDE 65

Applets as front-ends

Applet-Applet and JavaScript communication

◮ Adding the info of all applets in the page to a java.awt.TextArea called ’text’

Enumeration e = getAppletContext().getApplets(); while(e.hasMoreElements()) { text.append("\n" + ((Applet) e.nextElement()).getAppletInfo()); }

◮ Calling a method of another applet defined as

<applet codebase="." code="examples.Applet3.class" name="Paint" width="400" height="300" /> Applet other = getAppletContext().getApplet("Paint"); if(other != null) { ((Applet3) other).setInfo(new Date().toString()); }

◮ The same from Javascript

var paintapplet = document.applet.Paint; paintapplet.setInfo("Hello");

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 13 / 18

slide-66
SLIDE 66

Applets as front-ends

Accessing the HTML document through DOM

◮ Since Java 1.4 an applet can examine and modify the HTML document just like

JavaScript can

◮ DOM = Document Object Model, http://www.w3.org/DOM/ ◮ org.w3c.dom, org.w3c.dom.html

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 14 / 18

slide-67
SLIDE 67

Applets as front-ends

Accessing the HTML document through DOM

◮ Since Java 1.4 an applet can examine and modify the HTML document just like

JavaScript can

◮ DOM = Document Object Model, http://www.w3.org/DOM/ ◮ org.w3c.dom, org.w3c.dom.html ◮ Retrieving the document object:

com.sun.browser.dom.DOMService http://java.sun.com/j2se/1.5.0/docs/guide/plugin/developer_guide/ java_js.html#common_dom

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 14 / 18

slide-68
SLIDE 68

Applets as front-ends

Accessing the HTML document through DOM

◮ Since Java 1.4 an applet can examine and modify the HTML document just like

JavaScript can

◮ DOM = Document Object Model, http://www.w3.org/DOM/ ◮ org.w3c.dom, org.w3c.dom.html ◮ Retrieving the document object:

com.sun.browser.dom.DOMService http://java.sun.com/j2se/1.5.0/docs/guide/plugin/developer_guide/ java_js.html#common_dom

◮ Manipulating the object

  • rg.w3c.dom.html.HTMLDocument

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 14 / 18

slide-69
SLIDE 69

Applets as front-ends

Applet Security Restrictions

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-70
SLIDE 70

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-71
SLIDE 71

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-72
SLIDE 72

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-73
SLIDE 73

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

◮ They can’t open TCP connections (sockets) to any other host than the host

they are downloaded from

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-74
SLIDE 74

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

◮ They can’t open TCP connections (sockets) to any other host than the host

they are downloaded from

◮ Can’t read or write files (Though reading files via file URLs is possible) DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-75
SLIDE 75

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

◮ They can’t open TCP connections (sockets) to any other host than the host

they are downloaded from

◮ Can’t read or write files (Though reading files via file URLs is possible) ◮ Can’t start programs, or load native code DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-76
SLIDE 76

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

◮ They can’t open TCP connections (sockets) to any other host than the host

they are downloaded from

◮ Can’t read or write files (Though reading files via file URLs is possible) ◮ Can’t start programs, or load native code ◮ Can’t access certain System properties DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-77
SLIDE 77

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

◮ They can’t open TCP connections (sockets) to any other host than the host

they are downloaded from

◮ Can’t read or write files (Though reading files via file URLs is possible) ◮ Can’t start programs, or load native code ◮ Can’t access certain System properties ◮ java.awt.Window objects made by applets look different, to warn the user DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-78
SLIDE 78

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

◮ They can’t open TCP connections (sockets) to any other host than the host

they are downloaded from

◮ Can’t read or write files (Though reading files via file URLs is possible) ◮ Can’t start programs, or load native code ◮ Can’t access certain System properties ◮ java.awt.Window objects made by applets look different, to warn the user

◮ If the applet was loaded through the file:// protocol, security restrictions don’t

apply

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-79
SLIDE 79

Applets as front-ends

Applet Security Restrictions

◮ In principle, all java.* packages are accessible to applets ◮ Also you can use ARCHIVE to add other code ◮ But that doesn’t mean that applets have all the power of Java.

For example

◮ They can’t open TCP connections (sockets) to any other host than the host

they are downloaded from

◮ Can’t read or write files (Though reading files via file URLs is possible) ◮ Can’t start programs, or load native code ◮ Can’t access certain System properties ◮ java.awt.Window objects made by applets look different, to warn the user

◮ If the applet was loaded through the file:// protocol, security restrictions don’t

apply

◮ If the applet is signed, the browser (or java plug-in) should prompt the user that

the applet requires permission for one of the above operations. How to sign applets:

http://java.sun.com/j2se/1.5.0/docs/guide/plugin/ ← ֓ developer_guide/rsa_signing.html

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 15 / 18

slide-80
SLIDE 80

Applets as front-ends

Applet-servlet communication

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 16 / 18

slide-81
SLIDE 81

Applets as front-ends

Applet-servlet communication

◮ We can use a URL to connect to the HTTP server that the applet comes from

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 16 / 18

slide-82
SLIDE 82

Applets as front-ends

Applet-servlet communication

◮ We can use a URL to connect to the HTTP server that the applet comes from

◮ We can connect to any server on that host DD1335 (Lecture 7) Basic Internet Programming Spring 2010 16 / 18

slide-83
SLIDE 83

Applets as front-ends

Applet-servlet communication

◮ We can use a URL to connect to the HTTP server that the applet comes from

◮ We can connect to any server on that host

◮ There we can e.g. invoke a servlet (or even a CGI for that matter), that can do

something useful for our applet URL page = getCodeBase(); String protocol = page.getProtocol(); String host = page.getHost(); int port = page.getPort(); String servlet = "/servlet/SomeServlet"; URL dataUrl = new URL(protocol, host, port, servlet);

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 16 / 18

slide-84
SLIDE 84

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet);

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-85
SLIDE 85

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection();

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-86
SLIDE 86

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection(); conn.setUseCaches(false);

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-87
SLIDE 87

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection(); conn.setUseCaches(false); conn.setDoOutput(true);

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-88
SLIDE 88

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection(); conn.setUseCaches(false); conn.setDoOutput(true); String query = "firstName=" + URLEncoder.encode(firstName) + "&lastName=" + URLEncoder.encode(lastName) + "&emailAddress=" + URLEncoder.encode(emailAddress);

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-89
SLIDE 89

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection(); conn.setUseCaches(false); conn.setDoOutput(true); String query = "firstName=" + URLEncoder.encode(firstName) + "&lastName=" + URLEncoder.encode(lastName) + "&emailAddress=" + URLEncoder.encode(emailAddress); // we have to write the content length conn.setRequestProperty("Content-Length", String.valueOf(query.length()));

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-90
SLIDE 90

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection(); conn.setUseCaches(false); conn.setDoOutput(true); String query = "firstName=" + URLEncoder.encode(firstName) + "&lastName=" + URLEncoder.encode(lastName) + "&emailAddress=" + URLEncoder.encode(emailAddress); // we have to write the content length conn.setRequestProperty("Content-Length", String.valueOf(query.length())); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-91
SLIDE 91

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection(); conn.setUseCaches(false); conn.setDoOutput(true); String query = "firstName=" + URLEncoder.encode(firstName) + "&lastName=" + URLEncoder.encode(lastName) + "&emailAddress=" + URLEncoder.encode(emailAddress); // we have to write the content length conn.setRequestProperty("Content-Length", String.valueOf(query.length())); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // finished headers, now write the POST content conn.getOutputStream().write(query.getBytes());

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-92
SLIDE 92

Applets as front-ends

POST using URLConnection

URL dataURL = new URL(protocol, host, port, servlet); URLConnection conn = dataURL.openConnection(); conn.setUseCaches(false); conn.setDoOutput(true); String query = "firstName=" + URLEncoder.encode(firstName) + "&lastName=" + URLEncoder.encode(lastName) + "&emailAddress=" + URLEncoder.encode(emailAddress); // we have to write the content length conn.setRequestProperty("Content-Length", String.valueOf(query.length())); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // finished headers, now write the POST content conn.getOutputStream().write(query.getBytes()); // start reading BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 17 / 18

slide-93
SLIDE 93

Applets as front-ends

Audio/Media in Java

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 18 / 18

slide-94
SLIDE 94

Applets as front-ends

Audio/Media in Java

◮ Applets have direct support to play sound

AudioClip audioClip = getAudioClip(baseURL, relativeURL); audioClip.loop();

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 18 / 18

slide-95
SLIDE 95

Applets as front-ends

Audio/Media in Java

◮ Applets have direct support to play sound

AudioClip audioClip = getAudioClip(baseURL, relativeURL); audioClip.loop();

When you want it to stop playing (e.g. in the stop() method)

audioClip.stop();

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 18 / 18

slide-96
SLIDE 96

Applets as front-ends

Audio/Media in Java

◮ Applets have direct support to play sound

AudioClip audioClip = getAudioClip(baseURL, relativeURL); audioClip.loop();

When you want it to stop playing (e.g. in the stop() method)

audioClip.stop();

◮ To use this functionality outside applets

AudioClip audioClip = Applet.newAudioClip(completeURL);

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 18 / 18

slide-97
SLIDE 97

Applets as front-ends

Audio/Media in Java

◮ Applets have direct support to play sound

AudioClip audioClip = getAudioClip(baseURL, relativeURL); audioClip.loop();

When you want it to stop playing (e.g. in the stop() method)

audioClip.stop();

◮ To use this functionality outside applets

AudioClip audioClip = Applet.newAudioClip(completeURL);

◮ Details:

http://java.sun.com/docs/books/tutorial/sound/index.html http://java.sun.com/javase/6/docs/technotes/guides/sound/

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 18 / 18

slide-98
SLIDE 98

Applets as front-ends

Audio/Media in Java

◮ Applets have direct support to play sound

AudioClip audioClip = getAudioClip(baseURL, relativeURL); audioClip.loop();

When you want it to stop playing (e.g. in the stop() method)

audioClip.stop();

◮ To use this functionality outside applets

AudioClip audioClip = Applet.newAudioClip(completeURL);

◮ Details:

http://java.sun.com/docs/books/tutorial/sound/index.html http://java.sun.com/javase/6/docs/technotes/guides/sound/

◮ The latest in sound and video is the Java Media Framework

http://java.sun.com/javase/technologies/desktop/media/jmf/

DD1335 (Lecture 7) Basic Internet Programming Spring 2010 18 / 18