Java Network Load protocol (JNLP)

Source: Internet
Author: User

In the early stages of Java development, the focus was on client development. Support for Applet and secure download in languages
The release of the World Wide Web (WWW) seems to be a good idea. But the reality is that Java's greatest success lies in the server. The powerful functions and adaptability of Java have won the hearts of server developers. At the same time, client development lags behind. The thorny development issues limit the utility of the applet, and developers are forced to switch to browser-based thin clients.

Java Network launching protocol (JNLP, Java Network loading protocol) promises to change this situation. Through the development of JCP (Java Community process) JSR-56,
JNLP solves many previous issues of developing client-specific functions using Java. A JNLP client is an application or service that can load applications from the resources hosted on the network. If you use JNLP to package an application, a JNLP client can:

O test the application, install and use the correct version of JRE (Java Runtime Environment)
O load applications from browsers or desktops
O automatically downloads the latest version when a new application version appears.
O classes required for caching applications locally to accelerate startup
O can be run as an applet or application
O download the original library as necessary
O secure use of local resources such as file systems
O automatically locates and loads external dependent resources

Sun provides a reference implementation for implementing JNLP called Java Web Start (JWS. Let's use it to develop a simple application that uses jfc swing. To do this, you need to download jwsfrom http://java.sun.com/products/javawebstart. (Note: JDK's new version, jdk1.4, has built-in JWS and does not need to be downloaded .)

The following is the application 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. setdefaclocloseoperation (dispose_on_close );
F. setvisible (true );
F. addwindowlistener (New windowadapter (){
Public void windowclosed (windowevent e ){
System. Out. println ("shutting down ...");
System. Exit (0 );
}
});
}
}

The core of JNLP is a deployment list (deployment manifest ). It is done by using. JNLP
XML file with the extension (the JNLP specification is simply called "JNLP file "). To publish hellojnlp, You need to describe it in the JNLP file, as shown below:

<? 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 = "http://www.supcode.com/Article/html/4/43/2005/03/05/HelloJNLP.jpg"/>
</Information>
<Resources>
<J2se version = "1.2 +"/>
<Jar href = "hellojnlp. Jar"/>
</Resources>
<Application-Desc main-class = "hellojnlp"/>
</JNLP>

This list contains all the information that the client needs to download and use hellojnlp:

The codebase attribute of the o jnlp element specifies the top-level URL for searching application resources.
O information indicates the information that a JNLP user interface can display to the client.
The O j2se element indicates that the client must have version 1.2 or an updated j2se (TM ). (This is a major improvement for applet development, because it is often limited by the VM (Virtual Machine) provided by the browser ))
The O jar element specifies the location of the JAR file of the program relative to JNLP codebase.
The o Application-Desc element specifies the class to be run. You can add sub-elements to specify command line parameters or system attributes.

To publish this application to a Web server, perform the following steps:

1. Modify the codebase and hrefurl of JNLP to provide the appropriate URL for your web server.

2. Publish the JNLP file to the web server.

3. Compile and package hellojnlp. Java and release it to the web server. For example:

Jar CVF hellojnlp. Jar hellojnlp. Class hellojnlp $1. Class

4. Create an icon. You can use
Http://staff.develop.com/halloway/TechTips/May2001/http://www.supcode.com/Article/html/4/43/2005/03/05/HelloJNLP.jpg

5. Set mime-type Association for your web server:. JNLP maps to 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.

Run the application from the client. First, confirm that you have installed JWS. Then point to the JNLP file in the browser. The JWS client will download the JNLP file, download necessary resources, and load the application. What you see is the text "edit this text" displayed in an editing area ". If you have problems with configuring the Web server or cannot use the web server, you can
Http://staff.develop.com/halloway/TechTips/May2001/Hello.jnlp
Load this program.

Note that hellojnlp does not run as an applet in a browser, but as an independent application.

When you close the program, hellojnlp uses system. Out to print the message "shutting down...", but it is not visible on the console. The console is one of the many JWS settings with the default value "off. This is a set value that you can modify, as shown below:

1. Edit the javaws. cfg file in the JWS installation directory. Add a line "javaws. cfg. forceupdate = true ".
This causes JWS to automatically check the updated version before starting the application.

2. Run JWS. Choose File> preferences from the menu to enter the Advanced tab and select "show Java console ". (Since jdk1.4 is localized, JWS will display the Chinese interface, so it is automatically displayed as the corresponding Chinese) agree, select "log output" to output the log to the file you selected. It is useful when you need to capture system. Out and system. Err during debugging.

Hellojnlp displays an editor, but the editor content will be lost after you close the program. Add the following code to hellojnlp. Java automatically stores the editor status on 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 ();
}
}

(Note: For normal compilation, you must add the path javaws. jar to classpath, under the C:/program files/Java Web Start directory in Windows)
The jnlp api defines a set of services to bypass the security sandbox so that some common client operations can be used.
In the writeeditorcontents method, basicservice searches for the application code directory, and then
Persistenceservice caches the content in the editing area on the local hard disk and the content is typed into a URL relative to the application directory. The name/value provided by persistenceservice is similar to that provided by the browser for cookies. JWS implements this through a pair of things known as "muffins". Muffins are usually used to store big data. They should be used to cache small identifiers on the client. These identifiers can then be used to locate large information on the server.

Re-release the modified application on the Web server, and then try to run it from the client-still through the URL. If you do not have a web server, you can
Http://staff.develop.com/halloway/TechTips/May2001/Hello2.jnlp runs this new version

 

 

From: http://hi.baidu.com/ilotus_y/blog/item/09161fec7b24162263d09f63.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.