Comp2513
Daniel L. Silver, Ph.D.
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
Java an Object Oriented Programming language
Why Java and the Web?
Java Applets
2001 Daniel L. Silver 3
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
Operating System
*.exe
2001 4
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?
•
Prone to misuse of data and process
– COBOL
– Basic elements
• Not supportive of encapsulation,
– Fortran
– C cohesion and loose coupling
2001 Daniel L. Silver 5
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
Methods What are some examples of OO lang.?
A simple example … Hello_app.java
2001 Daniel L. Silver 6
Java is a relatively new language
“Green”, 1991 Sun Microsystems – dev. for use in consumer devices such as intelligent
TV controllers
– Object Oriented but simpler than C++
– Architecture neutral
– Real-time remote applications
– Portable, Reliable and Secure
2001 Daniel L. Silver 7
2001 program.class
program.java
Java Virtual Machine
Operating System
Compiler
Computer Hardware
Daniel L. Silver 8
Java source code
Java compiler javac hello_app.java
Java bytecode hello_app.class
Efficient because of bytecode
Java (JVM)
Interpreter java
2001
Bytecode compiler
Daniel L. Silver
Machine code hello_app.exe
9
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 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
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
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
vs
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 source code abc.java
Java compiler abc.class
Java applet bytecode
HTTP Server
App Server t
Interne
2001
Server
Daniel L. Silver
Web Browser
Java (JVM)
Interpreter
Client
15
A Simple Example:
HelloApplet.java
/ HelloApplet.html
…
Nothing is passed to the applet from the HTML
Anatomy of an Applet – fundamental methods:
– 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
2001 Daniel L. Silver 16
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
The Graphics object class allows you to do many things:
– setColor()
– fillRect()
– drawLine()
– drawRect()
– drawOval()
– drawImage()
2001 Daniel L. Silver 18
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
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
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
danny.silver@acadiau.ca