在 Applet 标记中嵌入 JNLP 文件

原文: https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/embeddingJNLPFileInWebPage.html

当使用 Java 网络启动协议(JNLP)部署 applet 时,Java Plug-in 软件在从网络下载 JNLP 文件后启动 applet。从 Java SE 7 发行版开始,您可以通过在网页本身中嵌入 JNLP 文件来减少 applet 启动所需的时间,以便在第一次加载 applet 时可以避免额外的网络请求。这将导致 applet 在 Web 浏览器上快速启动。

在网页中部署 applet 时,可以在jnlp_embedded参数中嵌入 Base64 编码的 JNLP 文件。 <jnlp>元素的属性应满足以下限制:

  • href属性应包含相对路径。
  • 不应指定codebase属性。这意味着代码库将从加载小程序的网页的 URL 派生。

以下步骤描述了如何在网页中嵌入 JNLP 文件以部署 applet。

  1. 为您的 applet 创建 JNLP文件。接下来显示一个示例文件。

    1. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    2. &lt;!-- href attribute contains relative path;
    3. codebase attribute not specified --&gt;
    4. &lt;jnlp href="dynamictree_applet.jnlp"&gt;
    5. &lt;information&gt;
    6. &lt;title&gt;Dynamic Tree Demo&lt;/title&gt;
    7. &lt;vendor&gt;Dynamic Team&lt;/vendor&gt;
    8. &lt;/information&gt;
    9. &lt;resources&gt;
    10. &lt;!-- Application Resources --&gt;
    11. &lt;j2se version="1.7+" /&gt;
    12. &lt;jar href=
    13. "dist/applet_ComponentArch_DynamicTreeDemo/DynamicTreeDemo.jar"
    14. main="true" /&gt;
    15. &lt;/resources&gt;
    16. &lt;applet-desc
    17. name="Dynamic Tree Demo Applet"
    18. main-class="appletComponentArch.DynamicTreeApplet"
    19. width="300"
    20. height="300"&gt;
    21. &lt;/applet-desc&gt;
    22. &lt;update check="background"/&gt;
    23. &lt;/jnlp&gt;
  2. 使用 Base64 方案对 JNLP 文件的内容进行编码。您可以使用任何 Base64 编码工具对 JNLP 文件进行编码。检查该工具的用法以创建具有 Base64 编码的字符串。可以使用的工具和网站的一些示例如下:

  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.

    1. &lt;script src="https://www.java.com/js/deployJava.js"&gt;&lt;/script&gt;
    2. &lt;script&gt;
    3. var attributes = {} ;
    4. &lt;!-- Base64 encoded string truncated below for readability --&gt;
    5. var parameters = {jnlp_href: 'dynamictree_applet.jnlp',
    6. jnlp_embedded: 'PCEtLSANCi8qDQogKiBDb ... bmxwPg=='
    7. } ;
    8. deployJava.runApplet(attributes, parameters, '1.6');
    9. &lt;/script&gt;

    一些编码工具可以将编码的字符串包装成几个 76 列的行。要在 JavaScript 代码中使用此多行属性值,请将属性值指定为一组连接字符串。如果直接使用<applet> HTML 标记部署 applet,则可以包含多行属性值。

在浏览器中打开 AppletPage.html,查看使用网页中嵌入的 JNLP 文件启动的动态树演示小程序。


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.


下载 Embedded JNLP 示例的源代码进行进一步实验。