Documentation

The Java™ Tutorials
Hide TOC
Embedding JNLP File in Applet Tag在Applet标记中嵌入JNLP文件
Trail: Deployment
Lesson: Deployment In-Depth
Section: Deployment Toolkit
Subsection: Deploying an Applet

Embedding JNLP File in Applet Tag在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. 使用Java网络启动协议(JNLP)部署小程序时,Java插件软件会在从网络下载JNLP文件后启动小程序。Beginning in the Java SE 7 release, you can reduce 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. 从JavaSE7版本开始,您可以通过将JNLP文件嵌入网页本身来减少小程序启动所需的时间,这样就可以在小程序第一次加载时避免额外的网络请求。This will result in applets launching quickly on the web browser.这将导致小程序在web浏览器上快速启动。

A Base64 encoded JNLP file can be embedded in the jnlp_embedded parameter when deploying an applet in a web page. 在网页中部署小程序时,可以在jnlp_embedded参数中嵌入Base64编码的JNLP文件。The attributes of the <jnlp> element should meet the following restrictions:<jnlp>元素的属性应满足以下限制:

The following steps describe how to embed a JNLP file in a web page to deploy an applet.以下步骤描述如何在网页中嵌入JNLP文件以部署小程序。

  1. Create a JNLP file for your applet. 为小程序创建JNLP文件。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. 使用Base64方案对JNLP文件的内容进行编码。You can use any Base64 encoding tool to encode the JNLP file. 可以使用任何Base64编码工具对JNLP文件进行编码。Check the usage of the tool to create a string with Base64 encoding. 检查该工具的使用情况,以创建Base64编码的字符串。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.


Note:  If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.

Note:  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 code for the Embedded JNLP example to experiment further.


Previous page: Deploying an Applet
Next page: Deploying a Java Web Start Application