Tutorials to .com

Tutorials to .com » Jsp » Application » Java network load Agreement (JNLP) and Java Web Start

Java network load Agreement (JNLP) and Java Web Start

Print View , by: iSee ,Total views: 41 ,Word Count: 2243 ,Date: Sun, 23 Aug 2009 Time: 4:07 PM

java network load Agreement (JNLP) and java Web Start
Translation: cherami
mail: cherami@163.net

Original: http://java.sun.com/jdc/JDCTechTips/2001/tt0530.html


Early in the java development, the focus was on developing the client. Language to download the applet and security support for the World Wide Web (WWW) release appears to be a good idea. But the reality is that java is the most successful server-side, java and adaptability of the power of server-side has won the hearts of developers. At the same time, the development of the client behind. The thorny issue of limiting the development of the effectiveness of applet, developers were forced to turn to thin client browser-based.

Java Network Launching Protocol (JNLP, java network load Protocol) promises to change the status quo. Through the JCP (Java Community Process) of the JSR-56 development,
JNLP previously used to solve a lot of java development for client functionality. A JNLP Client is an application or service that can host resources in the network load applications. If you use an application package JNLP, then a JNLP Client to:

o the application of detection, install and use the correct version of JRE (java runtime environment)
o from the browser or desktop application to load
o When a new version of the application arises automatically download the latest version.
o In order to accelerate the start speed of the cache on the local needs of the types of applications
o can be used as applet or application to run
o In the case of the need to download the original library
o use of secure file system such as a local resource
o automatically load external positioning and resource-dependent

Sun provides a realization of the JNLP called Java Web Start (JWS) to achieve the reference. Let us use it to develop a simple to use JFC Swing applications. In order to do this, you need to http://java.sun.com/products/javawebstart download JWS. (Translator's Note: JDK's JDK1.4 has built a new version of JWS, there is no need to download another.)

The following is the application of the code:

/ / File HelloJNLP.java
import javax.swing .*;
import java.awt .*;
import java.awt.event .*;

public class HelloJNLP extends JFrame (
public HelloJNLP () (
super ( "Hello JNLP");
String loadedFrom = this.getClass (). GetClassLoader (). ToString ();
JLabel jl = new JLabel ( "loaded by" + loadedFrom);
JEditorPane jtp = new JEditorPane ( "text / plain",
"Edit this text");
getContentPane (). add (jl, BorderLayout.NORTH);
getContentPane (). add (jtp, BorderLayout.CENTER);
)

public static void main (String [] args) (
JFrame f = new HelloJNLP ();
f.setBounds (100,100,325,250);
f.setDefaultCloseOperation (DISPOSE_ON_CLOSE);
f.setVisible (true);
f.addWindowListener (new WindowAdapter () (
public void windowClosed (WindowEvent e) (
System.out.println ( "Shutting down ...");
System.exit (0);
)
));
)
)

JNLP is a publication of the list of core (deployment manifest). It is a use. Jnlp so the xml file extension (JNLP specification referred to as simply "JNLP file"). To publish HelloJNLP, you need to describe it in the JNLP file, like the following:

<? xml version = "1.0" encoding = "UTF-8"?>
<! - File Hello.jnlp ->
<jnlp codebase = "http://staff.develop.com/halloway/TechTips/May2001"
href = "http://staff.develop.com/halloway/TechTips/May2001/Hello.jnlp">
<information>
<title> Hello </ title>
<vendor> Tech Tips Sample May 2001 </ vendor>
<icon href="HelloJNLP.jpg"/>
</ information>
<resources>
<j2se version="1.2+"/>
<jar href="HelloJNLP.jar"/>
</ resources>
<application-desc main-class="HelloJNLP"/>
</ jnlp>

This list contains the client will need to download and use all the information HelloJNLP:

o jnlpcodebaseURL。
o information element to point out a JNLP user interface can display information to the client.
o j2se element that the client must have a 1.2 or newer version of J2SE (tm). (This is the applet development for a large raise, because it is often limited by the browser to provide the VM (virtual machine))
o jar element of the JAR file that the procedures in relation to the jnlp codebase location.
o application-desc element pointed out that the type of operation. You can add child elements to specify command-line parameters or system properties.

To release this application to a web server, you need to perform the following steps:

1. Jnlp modify the codebase and hrefURL for your own web server suitable URL.

2. JNLP file will be posted to the web server.

3. HelloJNLP.java compiled and packaged and posted to the web server. For example:

jar cvf HelloJNLP.jar HelloJNLP.class HelloJNLP $ 1.class

4. HelloJNLP.jpg and will create an icon installed in the web server. You can use
http://staff.develop.com/halloway/TechTips/May2001/HelloJNLP.jpg

5. Set up your web server's mime-type association:. Jnlp mapped to the mime-type application / x-java-jnlp-file. For example, for Apache, add the following line to mime.types:

application / x-java-jnlp-file jnlp

Restart the web server.

From the implementation of the client application, first make sure you have installed JWS. Then a simple point in the browser files in the jnlp. JWS client will download the jnlp file, download the necessary resources, to load applications. You will see an edit in the text area to display "Edit this text". If you configure the web server problems or can not use the web server, you can
http://staff.develop.com/halloway/TechTips/May2001/Hello.jnlp
To load the process.

HelloJNLP attention as an applet is not running in a browser, but as a stand-alone applications.

When you close the program, HelloJNLP use System.out to print message "Shutting down ...", there is no console can be seen, however. JWS many console is the default setting is set to "off" one. This is one you can modify the settings of a value, as the following:

1. Editor installation directory in the JWS file javaws.cfg. Add a line "javaws.cfg.forceUpdate = true".
This can lead to start the application in the JWS automatically check for updates before the release.

2. Run JWS. Use the menu File-> Preferences, enter the Advanced tab and choose "Show Java Console". (Due to the JDK1.4 of this machine, JWS interface will be displayed in Chinese, so here because of the automatic display to the corresponding Chinese) agree, select "Log Output" will be output to the log file of your choice. When you are debugging and need to capture System.out and System.err is very useful.

Shows a HelloJNLP editor, content editor, however you will lose the closure procedure. The following code will be automatically added to HelloJNLP.java editor of the state will be stored to the client's hard disk:

/ / changes to HelloJNLP.java
import java.io. *;
import java.net .*;
import javax.jnlp .*;

/ / replace the constructor with this new version:
JEditorPane jtp;
public HelloJNLP () (
super ( "Hello JNLP, Second Version");
String loadedFrom = this.getClass (). GetClassLoader (). ToString ();
JLabel jl = new JLabel ( "loaded by" + loadedFrom);
jtp = new JEditorPane ( "text / plain", "Edit this text");
readEditorContents ();
getContentPane (). add (jl, BorderLayout.NORTH);
getContentPane (). add (jtp, BorderLayout.CENTER);

addWindowListener (new WindowAdapter () (
public void windowClosed (WindowEvent e) (
System.out.println ( "Shutting down ...");
try (
writeEditorContents ();
)
catch (Exception ex) (
System.out.println ( "Yoinks!");
ex.printStackTrace ();
)
System.exit (0);
)
));
)

/ / add these helper methods
private void writeEditorContents () throws
UnavailableServiceException, IOException (
System.out.println ( "writeEditorContents");
PersistenceService ps = (PersistenceService)
ServiceManager.lookup ( "javax.jnlp.PersistenceService");
BasicService bs = (BasicService)
ServiceManager.lookup ( "javax.jnlp.BasicService");
URL baseURL = bs.getCodeBase ();
System.out.println ( "CodeBase was" + baseURL);
URL editorURL = new URL (baseURL, "Editor");
try (
ps.create (editorURL, 1024);
)
catch (Exception e) (
e.printStackTrace ();
)
FileContents fc = ps.get (editorURL);
DataOutputStream os = new DataOutputStream (
fc.getOutputStream (false));
String s = jtp.getText ();
os.writeUTF (s);
os.flush ();
os.close ();
)

private void readEditorContents () (
try (
PersistenceService ps = (PersistenceService)
ServiceManager.lookup ( "javax.jnlp.PersistenceService");
BasicService bs = (BasicService)
ServiceManager.lookup ( "javax.jnlp.BasicService");
URL baseURL = bs.getCodeBase ();
URL editorURL = new URL (baseURL, "Editor");
FileContents fc = ps.get (editorURL);
DataInputStream is = new DataInputStream (fc.getInputStream ());
jtp.setText (is.readUTF ());
is.close ();
)
catch (Exception e) (
e.printStackTrace ();
)
)

(Translator's Note: the normal compile CLASSPATH need to add the path javaws.jar in the windows for the C: \ Program Files \ Java Web Start directory)
JNLP API defines a set of services to bypass the security sandbox to enable some of the normal operation of the client can use.
WriteEditorContents methods in, BasicService code to find the application directory, and then
District PersistenceService to edit the contents of the cache on the local hard disk, type these content and applications to a relative URL of the directory. PersistenceService provide the name / value pairs of data and the browser is very similar to the cookies. Through a pair of JWS called "muffins" things to achieve this, muffins when not used to store large data, they should be used for small cache on the client identifier. Then these identifiers can be used great positioning on the server information.

In the web server republish revised application, and then try to run it from the client - still through the URL. If you do not have web server, you can
http://staff.develop.com/halloway/TechTips/May2001/Hello2.jnlp run this new version. JWS automatic change detection procedure and run the new version. You can check the title of the string to prove this point, it will show "Hello JNLP, Second Version." Revise the content editing area, and then close it. When you load the program again, you will change. (When you first run the new version of the program you will see an exception in the console, this is the first time since readEditorContents can not find a muffin.)
(Translator's Note: In fact the first time when the abnormal operation of the procedure does not lead to the end zone so that the contents of editing client can not write, when next time you run the equivalent of the first run. That can not display this article characteristics, may be in the author's version of the JWS, the author is using the latest 1.0.1_02 (build b03))
JNLP provides a demonstration here of more than services. For example, you can:

o well how the control program download
o description of the various dependencies between JAR
o download and run the installer code
o code of the signature of the authority granted to annex
o the request of the designated version of the program or applet

To learn more about the situation of JNLP, please
http://java.sun.com/products/javawebstart/download-spec.html
Download JNLP specification.

JWS To learn more, please refer to http://java.sun.com/products/javawebstart/



Java Application / Other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.