Lecture note 13

Download Report

Transcript Lecture note 13

COMPS311F
Li Tak Sing
Applets
 An applet is any small application that performs one specific
task, sometimes running in the context of a larger program,
perhaps as a plug-in.
 A Java applet is an applet delivered to the users in the form
of Java bytecode.
 Applets are used to provide interactive features to web
applications that cannot be provided by HTML alone.
 They can capture mouse input and also have controls like
buttons or check boxes.
Applets
 An applet can also be a text area only, providing, for instance,
a cross platform command-line interface to some remote
system.
 However, applets have very little control over web page
content outside the applet dedicated area, of they are less
useful for improving the site appearance in general.
 Applets can also play media in formats that are not natively
supported by the browser.
Applets
 Java applets run at a speed that is comparable to (but
generally slower than) other compiled languages such as
C++, but many times faster than JavaScript.
 Java applets can use 3D hardware acceleration that is available
from Java. This makes applets well suited for non trivial
computation intensive visualizations.
 HTML pages may embed parameters that are passed to the
applet. Hence the same applet may appear differently
depending on the parameters that were passed.
Applets
 The same applet may appear differently depending on the
parameters that were passed.
 Since Java's bytecode is platform independent, Java applets
can be executed by broswers for many platforms, including
Microsoft Windows, Unix, Mac OC and Linux.
Technical information
 Java applets are executed ina sandbox by most web browsers,
preventing them from accessing local data like clipboard or
file system.
 The code of the applet is downloaded from a web server and
the browser either embeds the applet into a web page or
opens a new window showing the applet's user interface.
 The domain from where the applet executable has been
downloaded is the only domain to which the usual (unsigned)
applet is allowed to communicate. The domain can be
different from the domain where the surrounding HTML
document is hosted.
JApplet
 Java provides two classes for applets: Applet and JApplet.
JApplet .
 JApplet provides better look and control than Applet.
Methods of JApplet
 We do not use the JApple constructor to create a JApplet. It
is usually created by the Web browser.
 When a web page requests for an applet, it will first
download the code from the server, then the default
constructor of the applet will be called to create an instance
of the applet.
 Therefore, it does not make sense to have an applet with
non-default constructor.
 After the applet has been created, its init() method will be
called.
init() of JApplet
 Where should we put the initialization code of an applet?
The default constructor or init()?
 You can do it both ways. However, when the constructor is
called, the object may not have been created properly. For
example, calling a method of the applet is limited to the
current version of the method. If the applet is overridden
further, then the version of the method will still be the one
that is declared as the constructor.
init() of JApplet
 If in the constructor of A, it has invoked the meth() method,
the version to be invoked is that of A, not of B. It is because
at that time, it is still an A, not a B.
 On the other hand, if meth() is invoked in the init() method
of A and init() has not been overriddent in B, then the
version of meth() invoked would be that of B.
 Actually, it is always advisable to avoid invoking a member
function in a constructor of a class unless it is private or final.
init()
 Therefore, it is better to put initialization code in init()
instead of the default constructor.
 So there is usually no need to define the default constructor
of JApplet.
main()
 In all Java application, you need to have the main() method to
act as the main program. In JApplet, you do not need to
define the main method. The brower will know how to
instantiate the applet and execute the appropriate methods of
the JApplet when it is executed in a browser.
A simple applet
import javax.swing.*;
import java.awt.*;
public class MyJApplet extends JApplet {
public void init() {
JPanel p = new JPanel() {
public void paint(Graphics g) {
super.paint(g);
g.drawString("Hello World!", 10, 30);
}
};
this.getContentPane().add(p);
}
}
The html file for the applet
 In order to execute the applet in a browser, we need an
HTML file to tell the browser which applet we want to run.
 The basic format is like this:
<applet code="applet/MyJApplet.class" width="350"
height="350">
</applet>
The simple applet
 You can try the applet at:
 http://plbpc001.ouhk.edu.hk/~mt311f/examples/mt311201
0/build/classes/myjapplet.html
Things to note
 The MyJApplet is in the package applet. Therefore,
MyJApplet.class should be placed in a directory called applet.
The HTML file for the applet should be placed in the root
directory of applet.
 The MyJApplet.class file should be refered to as
"applet/MyJApplet.class" in the HTML file.
 So, if you have an applet with the full path name,
abc.def.GHI, then GHI.class should be placed in a directory
abc/def/ and the html file should be placed in the parent
directory of abc. The class should be referred as
"abc/def/GHI.class".
Other attributes of the <applet> tag.
 codebase
 This OPTIONAL attribute specifies the base URL of the applet-
-the directory that contains the applet's code. If this attribute is
not specified, then the document's URL is used. With this
option, the .class file and the .html file can be anywhere in the
system or even in different computers.
 For example, assume that abc.def.GHI.class is available at
http://ahost.com/xxx/abc/def/GHI.class, then, we can
specify the code base as http://ahost.com/xxx.
The html file with codebase
<applet code="applet/MyJApplet.class"
codebase="http://plbpc001.ouhk.edu.hk/~mt311f/examples
/mt3112010/build/classes"
width="350" height="350"></applet>
Archive
 This OPTIONAL attribute describes one or more achives
containing classes and other resources that will be
"preloaded". The archives are sparated by ",". For security
reasons, the applet's class loader can read only from the same
codebase from which the applet was started. This means that
archives must be in the same directory as, or in a
subdirectory of, the codebase.
 Entries like ../a/b.jar will not work unless explicitly allowed
for in the security policy file.
An example html file with the archive
option.
<applet code="applet/MyJApplet.class"
archive="dir/code.jar"
width="350" height="350"></applet>
 Here, we assume that in the directory that contains the html
file, there is a subdirectory called dir and in that subdirectory,
there is a file called code.jar.
 code.jar should then contain the .class file for
applet.MyJApplet .
Width and Hight of an applet
 This is done by specifying the width and height attributes of
the <applet> tag.
 The size is measured in terms of pixels.
Param
 We can specify some parameters to be passed to an applet.
Then these parameters can be accessed through the
getParameter() method of JApplet.
An applet that draws a circle according
to a parameter
public class Circle extends JApplet {
public void init() {
final int x=Integer.parseInt(this.getParameter("x"));
final int y=Integer.parseInt(this.getParameter("y"));
final int r=Integer.parseInt(this.getParameter("r"));
JPanel p=new JPanel() {
public void paint(Graphics g) {
super.paint(g);
g.drawOval(x-r, y-r, 2*r, 2*r);
}
};
this.getContentPane().add(p);
}
}
Network communication of JApplet
 As stated eariler, an applet is only allowed to communicate
with the host from which the applet was loaded.
 We can use the getCodeBase() method of JApplet to get the
code base.
 Note that there is another method called getDocumentBase().
This method returns the location where the HTML file in
which the applet is embeded.
 The code base and document base can be different. An
applet is only allowed to communicate with the computer at
the codebase, not the document base.
getCodeBase() method of JAplet
 public URL getCodeBase()
 This method return the URL from where the applet is loaded.
 URL has the getHost() method that allow us to get the host of
the URL.
Java Web start
 Java Web Start is a framework that allows users to start
application software for the Java Platform directly from the
Internet using a web browser.
 Unlike Java applets, Web Start applications do not run inside
the browser, and the sandbox in which they run need not
have as many restrictions, although this can be configured.
Web Start has an advantage over applets in that it overcomes
many compatibility problems with browsers' Java plugins and
different JVM versions. On the other hand, Web Start
programs cannot communicate with the browser as easily as
applets.
Java Network Launching
Protocol(JNLP)
 JNLP consists of a set of rules defining how exactly to
implement the launching mechanism. JNLP files include
information such as the location of the jar package file and
the name of the main class for the application, in addition to
any other parameters for the program. A properly configured
browser passes JNLP files to a Java Runtime Envvironment
(JRE) which in turn downloads the application onto the
user's machine and starts executing it.
Java Network Launching
Protocol(JNLP)
 Important Web Start features include the ability to
automatically download and install a JRE in the case where
the user dose not have Java installed, and for programmers to
specify which JRE version a given program needs in order to
execute.
 Any computer user can use JNLP by simply installing a JNLP
client (most commonly Java Web Start). The installation can
occur automatically such that the end user sees the client
launcher downloading and installing the Java application
when first executed.
Java Network Launching Protocol(JNLP)
 JNLP works in a similar fashion to how HTTP/HTML works
for the web. For rendering a HTML webpage, after the user
clicks on a weblink, the browser submits a URL to a
webserver, which replies with an HTML file. The browser
then requests the resources referred to by this file (images,
css), and finally renders the page once it has received enough
information. Page rendering usually starts before all
resources have downloaded; some resources not critical to
the layout of the page (such as images), can follow on
afterwards — or on request if the "Load Images
Automatically" browser-setting remains unset.
Java Network Launching
Protocol(JNLP)
 JNLP mirrors this process; in the same way that a Web
browser renders a webpage, a JNLP client "renders" a Java
app. After the user clicks on a weblink the browser submits a
URL to a webserver, which replies with a JNLP file (instead
of a HTML file) for the application. The JNLP client parses
this file, requests the resources specified (jar files), waits for
the retrieval of all required resources, and then launches the
application. The JNLP file can list resources as "lazy", which
informs the JNLP client that the application does not need
those resources to start, but can retrieve them later on
when/if the application requests them.
To create a Java Web Start
application in netbeans
 Create a normal Java project
 Select the properties of the project
 Click at Web Start in the Categories window.
 Select "Enable Web Start"
 In Codebase, select "Web Application Deployment"
 Select Self-signed.
To create a Java Web Start
application in netbeans
 A self-signed application is one that has a self-signed
certificate. Without a certificate, the Web Start application
would have limited rights like an applet. With a certificate, a
Web Start application would have rights like a local
application.
To create a Java Web Start application
in netbeans
 Then, create a normal Java application.
 Lets assume that we have create a Java class called
WebStart.java
 Then, netbeans will create the following files for you in the
dist directory:
 launch.html. This file is the html that that has the Web Start
application.
 launch.jnlp. This file is the configuration file for the Web Start
application.
 webstart.jar. This is the jar file that contains the required
resoruces like java code.
A simple Web Start Application
 You can try the program at:
http://plbpc001.ouhk.edu.hk/~mt311f/examples/webstar
t/dist/launch.html
A simple Web Start Application
public class WebStart extends JFrame implements ActionListener {
JPanel p=new JPanel();
JTextField text=new JTextField(5);
JButton button=new JButton("counter");
int count=0;
public WebStart() {
this.getContentPane().add(p);
p.add(text);
p.add(button);
text.setEditable(false);
pack();
setVisible(true);
button.addActionListener(this);
}
A simple Web Start Application
public void actionPerformed(ActionEvent e) {
text.setText(Integer.toString(count++));
}
public static void main(String st[]) {
WebStart s=new WebStart();
}
}
Deployment of the Web Start
Application
 After the application has been tested locally, you need to
deploy the application to a Web server. What you do is to
copy all the files in the dist folder of the project to
somewhere in the Web server. Then, you need to edit the
.jnlp file.
 You need to change the codebase to point to the web server.
Deployment of the Web Start
Application
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jnlp codebase="http://plbpc001.ouhk.edu.hk/~tsli/abc/"
href="launch.jnlp" spec="1.0+">
<information>
<title>abc</title>
<vendor>tsli</vendor>
<homepage href=""/>
<description>abc</description>
<description kind="short">abc</description>
Deployment of the Web Start
Application
</information>
<update check="always"/>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.5+"/>
<jar href="abc.jar" main="true"/>