Java Network Load Protocol (JNLP) and Java Web Start

zhaozj2021-02-08  280

Java Network Load Protocol (JNLP) and Java Web Start Translation: CheramiMail: Cherami@163.net Original: http://java.sun.com/jdc/jdctechtips/2001/tt0530.html

In the early days of Java development, the focus was placed in the client development. Language For Applets and Security Download support for the release of the World Wide Web (WWW), it looks a good idea. But reality is the biggest success of Java lies in server-side, Java's powerful features and adaptability to win the hearts of server-side developers. At the same time, the client's development is behind. Tough development problems limit the utility of Applets, developers being forced to turn to browser-based thin clients.

Java Network Launch Protocol (JNLP, Java Network Load Protocol) is committed to changing this status quo. The JNLP has solved a number of functions developed by Java developed by JAVA Community Process's JSR-56. A JNLP client is an application or a service that can load an application from a host in a network. If you use JNLP package an application, then a JNLP client can:

o For the application, install and use the correct version of JRE (Java runtime environment) o Automatically download the latest version when the application appears from the browser or desktop application. o To accelerate the class o required by the start-up speed in the machine cache application, the original library O automatically positions the original library O in a secure manner as an application in a secure manner as an applet or application. Load external dependency resources

Sun offers a reference implementation called JNLP called Java Web Start (JWS). Let us use it to develop a simple application using JFC Swing. To do this, you need to download JWS from http://java.sun.com/products/javawebstart. (Translator Note: JDK's new version JDK1.4 has built-in JWS, no additional download.)

Below is the code of the application:

// file hellojnlp.javaimport 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"); 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 release list (deployment manifest). It is an XML file that uses .jnlp to do extension (JNLP specification is simple to be called "JNLP file"). To post a Hellojnlp, you need to describe it in the JNLP file, just like this:

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></p> <p>This list contains all the information you need to download and use Hellojnlp:</p> <p>o JNLP element's CodeBase property indicates the top URL of the search application resource. o Information element indicates information that a JNLP user interface can be displayed to the client. o J2SE element indicates that the client must have a 1.2 version or updated J2SE (TM). (This is a big increase in Applet development because it is often limited to the VM (virtual machine) O JAR element provided by the browser) o JAR element to indicate the program's JAR file relative to the JNLP CodeBase location. o Application-Desc element indicates the class to run. You can add child elements to specify command line parameters or system properties.</p> <p>To publish this application to a web server, you need to do the following steps:</p> <p>1. Modify JNLP's codeBase and HREFURL for your own web server's appropriate URL. 2. Publish the JNLP file to the web server.</p> <p>3. Compile and package Hellojnlp.java and publish it to the web server. For example: JAR CVF Hellojnlp.jar Hellojnlp.class Hellojnlp $ 1.class 4. Create an icon hellojnlp.jpg and install it on the web server. You can use http://staff.develop.com/halloway/techtips/may2001/hellojnlp.jpg 5. Set your web server MIME-TYPE association: .jnlp map to MIME-TYPE Application / X-java-jnlp- File. For example, for Apache, add a row to mime.types: Application / X-Java-JNLP-File JNLP to restart the web server. Execute the app from the client, first confirm that you have installed JWS. Then simply point to the JNLP file in the browser. The JWS client will download the JNLP file, download the necessary resources, and load the app. What you see will be the text "Edit this text" displayed in an editing area. If you have questions on the web server or you can't use the web server, you can load this program from http://staff.develop.com/halloway/techtips/may2001/hello.jnlp.</p> <p>Note that hellojnlp is not run as an applet in the browser, but as a separate application.</p> <p>When you close the program, hellojnlp prints the message "Shutting Down ...", but there is no console visible. One of the defaults set to "OFF" in many settings of JWS. This is a value in a pair of settings you can modify, just like this:</p> <p>1. Edit the jws.cfg file in the JWS installation directory. Add a line "javaws.cfg.forceupdate = true". This will cause JWS to automatically check the updated version before starting the application. 2. Run JWS. Use the menu file-> preferences to enter the Advanced tab and select "Show Java Console". (Due to the localization in JDK1.4, JWS will display Chinese interface, so because it is automatically displayed as a corresponding Chinese) agreed, select "Log Output" to output the log to the file you selected. It is very useful when you are commissioning and need to capture system.out and system.err.</p> <p>Hellojnlp displays an editor, but the editor will be lost after you shut down. Add the following code to Hellojnlp.java automatically store the status of the editor to the client's hard drive:</p> <p>// Changes to hellojnlp.javaimport java.io. *; import java.net. *; import javax.jnlp. *;</p> <p>// 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);}});</p> <p>// 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 Editor = 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.PERSISISTENCESERVICE "); 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 ();}}</p> <p>(Translator Note: Normal compilation needs to add javaws.jar in ClassPath, under Windows C: / Program Files / Java Web Start Directory) JNLP API defines a set of services to bypass safe sandbox Some usual client operations can be used. In the WriteEditorContents method, the BasicService finds the application's code directory, then PersistenceService caches the contents of the editing area on the local hard disk, which is typed to the URL relative to the application directory. The name / value provided by PersistenceService is similar to the cookies of the data and browsers. JWS implements this through a pair of things called "Muffins", and Muffins is often used to store big data, and they should be used to store small identifiers in the client. These identifiers can then be used to locate large information on the server. Reploze the modified application on the web server, then try to run it from the client - still through the URL. If you don't have a web server, you can run this new version from http://staff.develop.com/halloway/techtips/may2001/hello2.jnlp. The JWS automatic detecting program is changed and running new version. You can confirm this by checking the strings of the title bar, which will now display "Hello JNLP, SECOND VERSION." To modify the contents of the editing area, then close it. When you load this program again, your changes will appear. (When you first run a new version of the program, you will see an exception in the console, because ReadEditorContents can't find Muffin for the first time.) (Translator Note: Actually the exception that occurs during the first runtime It is not possible to end normally, so that the content of the editing area cannot be written to the client. The next runtime is equivalent to the first run. That is, this program cannot display the characteristics of the article, may be related to the version of the JWS, the author used It is the latest 1.0.1_02 (Build B03)) JNLP provides more services than here. For example, you can:</p> <p>o Good control program How to download o Describe the dependencies between the various JARs O download and run the unit code installer O request the privilege of the signed code O request to specify the program or applet</p> <p>To learn more about JNLP, please go to http://java.sun.com/products/javawebstart/download-spec.html to download JNLP specifications.</p> <p>To learn more about JWS, please refer to http://java.sun.com/products/javawebstart/</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-1969.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="1969" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.056</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'Xi2_2FgHz7vDVGVAWvIMZRYW4Hhs2prwlUpxGLTl5rSQnqX_2BSo_2BLDiSdOXZuVxXci2oWdNrTr_2FTgHI0dHC4JEnpg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>