在 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。
为您的 applet 创建
JNLP
文件。接下来显示一个示例文件。<?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>
使用 Base64 方案对 JNLP 文件的内容进行编码。您可以使用任何 Base64 编码工具对 JNLP 文件进行编码。检查该工具的用法以创建具有 Base64 编码的字符串。可以使用的工具和网站的一些示例如下:
- UNIX 命令 -
base64
,uuencode
- 网站 - Base64 编码和解码, Base64 编码器
- UNIX 命令 -
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>
一些编码工具可以将编码的字符串包装成几个 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 示例的源代码进行进一步实验。