文档

Java™ 教程-Java Tutorials 中文版
在 Applet 标记中嵌入 JNLP 文件
Trail: Deployment
Lesson: Deployment In-Depth
Section: Deployment Toolkit
Subsection: Deploying an Applet

在 Applet 标记中嵌入 JNLP 文件

When applets are deployed by using the Java Network Launch Protocol (JNLP), the Java Plug-in software launches the applet after downloading the JNLP file from the network. Beginning in the Java SE 7 release, you can reduce the the time it takes for applets to launch, by embedding the JNLP file in the web page itself so that an additional network request can be avoided the first time the applet is loaded. This will result in applets launching quickly on the web browser.

A Base64 encoded JNLP file can be embedded in the jnlp_embedded parameter when deploying an applet in a web page. The attributes of the <jnlp> element should meet the following restrictions:

The following steps describe how to embed a JNLP file in a web page to deploy an applet.

  1. Create a JNLP file for your applet. A sample file is shown next.
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- href attribute contains relative path;
         codebase attribute not specified -->
    <jnlp href="dynamictree_applet.jnlp">
        <information>
            <title>Dynamic Tree Demo</title>
            <vendor>Dynamic Team</vendor>
        </information>
        <resources>
            <!-- Application Resources -->
            <j2se version="1.7+" />
            <jar href=
                "dist/applet_ComponentArch_DynamicTreeDemo/DynamicTreeDemo.jar" 
                 main="true" />
        </resources>
        <applet-desc 
             name="Dynamic Tree Demo Applet"
             main-class="appletComponentArch.DynamicTreeApplet"
             width="300"
             height="300">
         </applet-desc>
         <update check="background"/>
    </jnlp>
    
  2. Encode the contents of the JNLP file using the Base64 scheme. You can use any Base64 encoding tool to encode the the JNLP file. Check the usage of the tool to create a string with Base64 encoding. Some examples of tools and web sites that may be used are as follows:
  3. When deploying the applet in a web page, specify the jnlp_embedded parameter with it's value set to the Base64 encoded JNLP string. Make sure to include only the actual Base64 bytes without any encoding tool specific headers or footers.
    <script src="https://www.java.com/js/deployJava.js"></script>
    <script>
        var attributes = {} ;
        <!-- Base64 encoded string truncated below for readability -->
        var parameters = {jnlp_href: 'dynamictree_applet.jnlp',
            jnlp_embedded: 'PCEtLSANCi8qDQogKiBDb ... bmxwPg=='
        } ;
        deployJava.runApplet(attributes, parameters, '1.6');
    </script>
    

    Some encoding tools may wrap the encoded string into several 76-column lines. To use this multi-line attribute value in JavaScript code, specify the attribute value as a set of concatenated strings. You can include the multi-line attribute value as is if the applet is deployed directly with the <applet> HTML tag.

Open AppletPage.html in a browser to view the Dynamic Tree Demo applet that is launched by using the JNLP file embedded in the web page.


注意:如果你没有看到 applet 正在运行,则需要至少安装 Java SE Development Kit (JDK) 7 版本。

注意: If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.

Download source codefor the Embedded JNLP example to experiment further.


Previous page: Customizing the Loading Screen
Next page: Deploying a Java Web Start Application