Download Lecture 10 Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

URL redirection wikipedia , lookup

Transcript
Introduction to Java 2
Programming
Lecture 10
Applets
Overview
• Introduction to Applets
– The Rules for Applets
– The Applet Lifecycle
• Writing Applets
– Creating applets
– Deploying applets
• Examples: Interacting with the browser
– Parameters
– Displaying status messages
– Showing documents
Introduction to Applets
• Applets are applications that are deployed over the
Internet
– Designed to run inside a browser
– Are embedded in HTML pages
– Core part of Java
• Not are popular as they were (or forecasted to be)
– Patchy browser support
– Can be slow to download
– Macromedia Flash, etc. offer similar functionality
Introduction to Applets
•
•
•
•
But do provides a number of benefits…
Easy to deploy (“web components”)
No need for installation or upgrades
Provide more sophisticated functionality than a
web page/form
• Allow for proprietary client-server protocols
• Re-use code from traditional applications
• Very secure
The Rules for Applets
• An applet cannot (usually) do the following:
• Cannot load libraries or define native
methods
• Cannot read or write files on the client
• Cannot make network connections except to
the server it came from
• Cannot start any program on the client
• Cannot read certain system properties
• Cannot ‘pretend’ to be a local application
– Applet windows look different
The Rules for Applets
• Ensures that an applet cannot damage the client
– Otherwise opens potential for viruses, security
breaches, trojan horses, etc
– Applets are considered to be untrusted code
• Rules are enforced by a Security Manager
– Installed by the JVM in the browser
• The rules are known as a security policy
• Alternate policies can be used on request
– But only if the user decides to trust the code
The Applet Lifecycle
• Applets are loaded, started and managed by the
browser
• Browser is a ‘container’ that provides services to
the applet
– Similar to Robots in the Robocode arena
• Browser drives the applet through life-cycle
methods
– The methods mark ‘milestones’ in the applet life
– Instruct it to carry out some basic operations
– Form a contract between the browser and the applet
• (Aside: this is a common pattern used in many
Java frameworks in various forms)
The Applet Lifecycle
• Four basic lifecycle methods
– Defaults provided by java.applet.Applet
• init() – Initialises the applet when (re-)loaded
– Perform initialisation here rather than constructor
– Guarantees complete environment is available (e.g.
parameters)
• start() – Starts the applet running, after
loading or user revisits page
• stop() – Stops the applet running, when user
leaves page or quits
• destroy() – Perform final clean-up before its
unloaded
The Applet Lifecycle
init()
Initialised
start()
Running
stop()
start()
Stopped
destroy()
Destroyed
Writing Applets
• Writing applet involves creating a sub-class of
– java.applet.Applet, or
– javax.swing.JApplet (recommended)
• Remember, JApplet is a top-level Swing
container
– can add usual Swing components
• Applet base class provides a number of useful
methods
• Applets also have a context object, that provides
other functionality
– E.g. driving the browser, communicating with other
applets
Writing Applets
Useful Methods
• getParameter() – get a parameter set in the
web page
• showStatus() – show a message in the status
bar
• getImage() – load an image
• getAudioClip(), play() – play a sound
file
• getAppletContext() – get context object
• getAppletContext().showDocument()
– instruct the browser to show another webpage
Writing Applets
• Tip for development/debugging:
– Use the appletviewer tool for viewing and
testing applets
– Easier to control than in a browser
– Avoids problems with caching of applets
• Example…A Basic Applet
Deploying Applets
• Applets are embedded into web pages with the
<applet> tag
– Instructs the browser to display an applet in that
location of the web page
• The applet tag can be used to set several properties
about the applet
– the class to load and run as an applet
– height and width
– Location of class files (codebase)
Deploying Applets
<applet
name=“MyApplet”
code=“AppletSubclass.class”
width=“anInt”
height=“anInt”
codebase=“http://where.applet.lives”>
<param name=“parameter1Name”
value=“aValue”/>
<param name=“parameter2Name”
value=“anotherValue”/>
Your browser is not Java enabled!
</applet>
Deploying Applets
<html>
<body>
<h1>The Basic Applet</h1>
<applet
code="intro2java.applet.BasicApplet.class"
width="100"
height="100">
</applet>
</body>
</html>
Writing Applets
• Beware of CLASSPATH!
– Applets classes are loaded from the same
directory as the HTML page
– Unless an alternate directory is set with the
codebase attribute
Examples
•
•
The Hello World Applet
Interacting with the browser
1.
2.
3.
4.
Getting parameters
Showing a status message
Showing other web pages
The Calculator as an applet