Download Java Web Start

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
no text concepts found
Transcript
Java Web Start
2002 Art Gittleman
When Java first appeared, it intrigued users with applets on the client that added
dynamic content to web pages. However, difficulties with browser support for Java,
download times, and insufficient user interface components dampened the initial
enthusiasm for applets.
The Swing component library greatly improved and added to the components
available with the AWT. The Java Plug-in, described in Chapter 12, allows us to run
applets in a browser using the latest Java version. The Java Web Start utility, available
from Sun at java.sun.com/producst/javawebstart/, allows us to launch an
application or an applet from a browser. It will save an application locally so that it can
be run again without using the browser.
Java Web Start must be installed on the client. The application's code must be
downloaded the first time, but thereafter it starts up from the client. Java Web Start will
check the source location for updates and install a new version if available.
We launch both an application an applet.
Launching an Application
For the application we use the program Hashing.java which computes a few
hash values.
Hashing.java
/* Uses a Collection. Displays a few hash codes.
*/
import java.util.*;
import java.io.*;
import java.text.*;
public class Hashing {
public static void main(String[ ] args) {
Collection start = new HashSet();
start.add("fourscore");
start.add("and");
start.add("seven");
start.add("years");
Iterator iterator = start.iterator();
System.out.println("The elements of start are: ");
while (iterator.hasNext()){
String next = (String)iterator.next();
int hashCode = next.hashCode();
String hash = String.valueOf(hashCode);
String hashMod10 = String.valueOf(hashCode % 10);
System.out.println("\t" + next + spaces(12-next.length()) +
spaces(12-hash.length()) + hash +
spaces(12-hashMod10.length()) + hashMod10);
}
}
public static StringBuffer spaces(int amount) {
StringBuffer s = new StringBuffer(amount);
for (int i =0; i < amount; i++)
s.append(' ');
return s;
}
}
--------------------------------------------------------------------------------------------------------To launch an application, we need to package all the necessary files in a JAR file.
Then Java Web Start can cache the JAR file locally. After compiling, we use the
command
jar cf Hashing.jar Hashing.class gittleman.gif
to create Hashing.jar.
Java Web Start uses the Java Network Launching Protocol. We create an XML
file with the .jnlp extension that contains the necessary launch information. For the
hashing program we use
Hashing.jnlp
<?xml version="1.0" encoding="utf-8"?>
<!-- JNL File for Hashing -->
<jnlp
codebase="http://192.168.0.1:8100/"
href="Hashing.jnlp">
<information>
<title>Hashing</title>
<vendor>Art Gittleman</vendor>
<description>Hashing</description>
<icon href="gittleman.gif"/>
<offline-allowed/>
</information>
<resources>
<j2se version="1.2+"/>
<jar href="Hashing.jar"/>
</resources>
<application-desc main-class="Hashing"/>
</jnlp>
The <jnlp> tag include the codebase attribute specifying the base directory to locate
the files. The href attribute gives the path, relative to the codebase, to the JNLP file.
We use the three nested tags, <information>, <resources>, and
<application-desc>. Most of the tags nested in the <information> tag are
self-explanatory. The <offline-allowed/> tag allows the application to be run
offline, and allows cuts short the attempt to update the client after a few seconds. A
client required to run online will always check for a possible update.
The <resources> tag includes the nested <j2se> and <jar> tags. In the
version attribute of the <j2se> tag we specify that the application requires version
1.2 and above of the standard edition. The <jar> tag includes the href attribute that
gives the relative path to the JAR file for the application. The main-class attribute of
the <application-desc> tag names the class containing the main method.
We need to configure our web server to associate the correct MIME type with .jnlp
files, so the browser will process them properly. All files with the .jnlp extension should
have the application/x-java-jnlp-file MIME type. Using the JRun server, we add
<mime-mapping>
<extension>jar</extension>
<mime-type>application/x-java-archive</mime-type>
</mime-mapping>
<mime-mapping>
<extension>jnlp</extension>
<mime-type>application/x-java-jnlp-file</mime-type>
</mime-mapping>
to the web.xml file found in
%JRUN_HOME%\servers\default\default-app\WEB-INF
The first <mime-mapping> tag sets the MIME type for JAR files.
To deploy this application we create an HTML file
Hashing.html
<html>
<body>
<a href="http://java.sun.com/products/javawebstart">
Download Java Web Start</a><br>
<h3>Illustrating Java Web Start<h3><br>
<a href="Hashing.jnlp">
Launch a hashing example
</a>
</body>
</html>
and place it, Hashing.jar, and Hashing.jnlp in the directory
%JRUN_HOME%\servers\default\default-app
Figure 1 shows the initial web page, and Figure 2 shows the result of the launch.
Figure 1 Browsing Hashing.html
Figure 2 Executing the hashing program
Launching an Applet
The CheckSecurity applet of Example 12.2 tests the use of a security manager. The
Internet Explorer 5 browser denied four of the five requests because of security
violations. In Section 12.2 we signed the applet, creating the file SignedSecurity.jar. We
shall use Java Web Start to deploy this signed applet. By adding
<security>
<all-permissions/>
</security>
to the JNLP file we request the signed applet to grant all permissions. The JNLP file is
Security.jnlp
<?xml version="1.0" encoding="utf-8"?>
<!-- JNL File for Security -->
<jnlp
codebase="http://192.168.0.1:8100/"
href="Security.jnlp">
<information>
<title>Security</title>
<vendor>Art Gittleman</vendor>
<description>Security</description>
<icon href="gittleman.gif"/>
<offline-allowed/>
</information>
<resources>
<j2se version="1.2+"/>
<jar href="SignedSecurity.jar"/>
</resources>
<security>
<all-permissions/>
</security>
<applet-desc
name="Security"
main-class="CheckSecurity"
width = "300"
height = 200 />
</jnlp>
This JNLP file uses the <applet-desc> tag instead of the <application-desc>
tag used by the hashing application. The HTMl file is
Security.html
<html>
<body>
<h3>Illustrating Java Web Start<h3><br>
<a href="Security.jnlp">
Launch a security example
</a>
</body>
</html>
When we launch this applet Java Web Start provides the security warning shown in
Figure 3.
Figure 3 A security warning
Pressing the Start button causes Java Web Start to execute the applet. Pushing the
Details button shows the details of the certificate.
Figure 4 The certificate details
Figure 5 shows the result of granting all permissions.
Figure 5 Granting all permissions to an applet