Java, Applets and JavaScript

advertisement
Comp2513
Java and Applets
Daniel L. Silver, Ph.D.
Objectives
To introduce the Java programming
language and its fit with the Web
 To discuss the use of Applets as a part of the
E-Commerce infrastructure
 References: Chapter 3

2001
Daniel L. Silver
2
Outline
Java an Object Oriented Programming
language
 Why Java and the Web?
 Java Applets

2001
Daniel L. Silver
3
Programming Languages
A programming language adheres to a
specified syntax that is accepted by either
an interpreter or a compiler.
 What is the difference between an
interpreter and a compiler?

Basic code
C code
Interpreter
Compiler
*.exe
Operating System
2001
Computer Hardware
Daniel L. Silver
4
Function-Oriented Programming

Traditional programming languages are known as
function-oriented. Why?
– Consider the movement of data from function to
function
– Data and processing are considered separately

What are some examples of function-oriented
languages?
–
–
–
–
2001
COBOL
Basic
Fortran
C
• Prone to misuse of data and process
elements
• Not supportive of encapsulation,
cohesion and loose coupling
Daniel L. Silver
5
Object-Oriented Programmng
Combines data and processes together into
objects
 An object is an entity that can contain data
(attributes, properties) and can manipulate
data using functions (methods)
Attributes
 An object has state and behaviour
 What are some examples of OO lang.? Methods
 A simple example … Hello_app.java

2001
Daniel L. Silver
6
Java: An OOP Language
Java is a relatively new language
 “Green”, 1991 Sun Microsystems – dev. for
use in consumer devices such as intelligent
TV controllers

–
–
–
–
2001
Object Oriented but simpler than C++
Architecture neutral
Real-time remote applications
Portable, Reliable and Secure
Daniel L. Silver
7
Java and the
Java Virtual Machine
program.java
program.class
Java Virtual Machine
Compiler
Operating System
Computer Hardware
2001
Daniel L. Silver
8
Java Translation and Execution
Java source
code
hello_app.java
Java compiler
Java bytecode hello_app.class
javac
Bytecode
compiler
Efficient
Java (JVM)
because of Interpreter
bytecode
java
2001
Machine code hello_app.exe
Daniel L. Silver
9
Java and the Web
In 1995 the first HotJava browser was
demoed at SunWorld exhibition
 It could download programs called applets
from a the web and run them locally
 Provided animation and interaction
 “Write once, run anywhere”
 By 1996 both Netscape and MS supported
Java within their browsers

2001
Daniel L. Silver
10
Java and the Web
Java has been designed for the Web
 It has two attributes that make it suitable:
– Security: An assortment of security features

that guarantee that no evil applets can be
written and assist in the writing of good code
– Portability: Applications and Applets can run
on Windows, Unix, Linux, Mac, IBM midrange and mainframe
2001
Daniel L. Silver
11
Java Applet Security



Programs that come from over the network can be
malicious (destroy data on your PC)
Java was designed to prevent malicious behaviour
Two primary safety features:
– Signatures – an applet can be signed and a browser can
be set up to accept only trusted applet authors
– Secuirty Priviledges – by default an applet runs in the
“sandbox” where I/O is limited to the keyboard/mouse
and the display, trusted applets can be give higher level
priveledges (e.g. disk I/O )
2001
Daniel L. Silver
12
Java and the Web

Reasons why Java has become the fastest
growing programming language of all time:
– It is an object-oriented language
– Contains a vast library of software (object
classes and methods)
» Java Development Kit (J2SE SDK)
» Objects that have been developed and tested
» Imported for use at the beginning of a program
– A good first language to learn ... Why?
2001
Daniel L. Silver
13
Java Applications vs Applets
There are two classes of Java programs:
 Applications - such as Hello_app.java
– I/O is by default with console and character oriented
– Graphical I/O is an option that is commonly taken

Applets – as we shall see
– Run by a Web browser using an imbedded Java
interpreter
– Graphical I/O – characters, diagrams
2001
Daniel L. Silver
14
Java Translation and Execution
Java source
code
abc.java
Interne
t
Java compiler
abc.class
HTTP Server
App Server
Java applet
bytecode
Web Browser
Java (JVM)
Interpreter
Server
Client
2001
Daniel L. Silver
15
Java Applets
A Simple Example:
 HelloApplet.java / HelloApplet.html …
 Nothing is passed to the applet from the HTML
 Anatomy of an Applet – fundamental methods:
–
–
–
–
–
2001
init() – invoked once when applet is first loaded
start() – invoked each time applet becomes visible
stop() – invoked each time applet becomes invisible
paint() – display of text and graphics
destroy() – invoked once when applet is exited
Daniel L. Silver
16
Java Applets
Parameter Passing Example:
 FirstApplet.java / FirstApplet.html …
 <PARAM> tag is used to pass parameters from
HTML to Java program at run time
 <PARAM> has two attributes:
– NAME - name of parameter being passed
– VALUE – value of parameter being passed
- e.g.:
<PARAM NAME="message" VALUE="Message from HTML File">
2001
Daniel L. Silver
17
Java Applets

The Graphics object class allows you to do
many things:
–
–
–
–
–
–
2001
setColor()
fillRect()
drawLine()
drawRect()
drawOval()
drawImage()
Daniel L. Silver
18
Java Applets
Threads and Event Handling Example:
 AnimationApplet.java /
AnimationApplet.html …
 Thread – a portion of a Java program that
executes independently, e.g.:
– Thread 1 - allows animation to occur on the
browser window
– Thread 2 – captures input from the keyboard
2001
Daniel L. Silver
19
Java Applets
Threads and Event Handling Example:
 Event Handling is used within programs to
associate events such as mouse clicks to
revelent portions of code
 In AnimationApplet.java a mouseDown()
event is used to start and stop the scrolling
message
2001
Daniel L. Silver
20
Java Applets

A more advanced example – link.html
– Provides sources of further information on Java
and Applets

Notice how in link.java:
– <param> is used in this example
– showDocument(theURL,targetFrame) is used to
link to a URL
2001
Daniel L. Silver
21
THE END
[email protected]
Download
Related flashcards
Create Flashcards