Java Applets PDF
Java Applets PDF
SwIG Jing He
Outline
What is Java?
Java Applications Java Applets Java Applets Securities
Summary
What is Java?
Java was conceived by James Gosling at Sun Microsystems Inc. in 1991 Java is platform independent language Java programming is a/an object-oriented programming.
myPurse.addMoney(theATM. withdrawMoney(myATMCard,1000));
encapsulation you do not need to know how the ATM works inside. inheritance you can easily create class ATMWithClocks extending class ATM. The new class inherits the methods of the ATM class.
Java Architecture
source code JVM
Compiler
byte code
Computer
programmer
user
Java Architecure
source code
portability security
JVM
Compiler
byte code
Computer
programmer
user
speed
Why Java?
simple portable secure free
slow
Server
Learning Java
language libraries book, lectures
documentation http://java.sun.com/docs/ examples on the web (problem often old version of Java)
HelloWorld.java:
public class HelloWorld { public static void main (String[] args) { System.out.println(Hello, World); } } Compile HelloWorld.java javac HelloWorld.java Output: HelloWorld.class Run java HelloWorld 15 Output: Hello, World
Prepare the file HelloWorld.java using an editor Invoke the compiler: javac HelloWorld.java This creates HelloWorld.class Run the java interpreter: java HelloWorld
What is an applet?
PIG PIGLET
APPLE APPLET
What is an applet?
An applet is a small Java program that is embedded and ran in some other Java interpreter program such as
a Java technology-enabled browser Suns applet viewer program called appletviewer
Applet
TCP/IP (socket)
TCP/IP (HTTP)
Internet
TCP/IP (HTTP)
Client
TCP/IP (socket)
Web Server
HelloWorld.class
Applet Execution - 1
An applet program is a written as a inheritance of the java.Applet class There is no main() method in an Applet.
Applet Execution - 2
init: This method is intended for whatever initialization is needed for an applet. start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages. stop: This method is automatically called whenever the user moves away from the page containing applets. This method can be used to stop an animation. destroy: This method is only called when the browser shuts down normally.
Ref: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html/
Applet Execution - 3
The applet is running and rendered on the web page. Every Applet needs to implement one or more of the init(), the start( ) and the paint( ) methods. At the end of the execution, the stop( ) method is invoked, followed by the destroy( ) method to deallocate the applets resources.
browser calls init on that applet, once browser calls start on that applet
init()
start()
do some work
stop() destroy()
Applet's Attributes
Attribute Explanation Example
Code
Width height Codebase alt name
Align(top,left,right,bottom)
Code=applet0.class
Width=300 Height=60 Codebase=/applets Alt=menu applet Name=appletExam Align=right
HTML
<html> <head> <title> Hello World Applet </title> </head> <body> <applet code="HelloWorld.class width=300 height=200> </applet> </body> </html>
Your Turn!
History of an Applet
notepad Hello.java notepad Hello.html javac Hello.java produces Hello.class appletviewer Hello.html browser Hello.html
what to draw
where to draw it
Save as Hello.java
History of an Applet
notepad Hello.java notepad Hello.html javac Hello.java produces Hello.class appletviewer Hello.html browser Hello.html
History of an Applet
notepad Hello.java notepad Hello.html javac Hello.java produces Hello.class appletviewer Hello.html browser Hello.html
Hello
Applet Security
For security reasons, applets that are loaded over the network have several restrictions. an applet cannot ordinarily read or write files on the computer that it's executing on. an applet cannot make network connections except to the host that it came from.
Ref: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html/
Applets cant with any host other than the originating server
Applets cant read/write to local computers file system
Applets cant find any information about the local computer All java-created pop-up windows carry a warning message Stability depends on stability of the clients web server Performance directly depend on clients machine
Automatically integrated with HTML; hence, resolved virtually all installation issues. Can be accessed from various platforms and various java-enabled web browsers. Can provide dynamic, graphics capabilities and visualizations Implemented in Java, an easy-to-learn OO programming language
Safe! Because of the security built into the core Java language and the applet structure, you dont have to worry about bad code causing damage to someones system
Can be launched as a standalone web application independent of the host web server
Summary
An applet is a Java class Its code is downloaded from a web server It is invoked by a browser when it scans a web page and encounters a class specified with the APPLET tag For security reason, the execution of an applet is normally subject to restrictions:
applets cannot access files in the file system on the client host Applets cannot make network connection exception to the server host from which it originated
Resources
http://java.sun.com/
Online documentation and tutorials Integrated development environment (IDE) for nothing in particular Java[tm] development tools (JDT) (comes with Eclips)
http://www.eclipse.org/
C/C++ extension in progress AspectJ support Windows, Linux, and Mac.... FREE!