如何在浏览器和 Java Web Start 小程序之间进行通信 [英] How to communicate between browser and Java Web Start applet

查看:19
本文介绍了如何在浏览器和 Java Web Start 小程序之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现状

我们目前使用一个小程序来执行一些操作,然后它会重定向当前页面.在其核心中,您可以看到小程序如下所示:

We currently use an applet to perform some operations, after which it redirects the current page. In its core, you could see the applet as the following:

public class ExampleApplet extends Applet {
    @Override
    public void init() {
        Button redirect = new Button("Redirect");
        this.add(redirect);
        final String target = this.getParameter("targetPage");
        redirect.addActionListener((ActionEvent e) -> {
            try {
                getAppletContext().showDocument(new URL(target), "_parent");
            } catch (MalformedURLException ex) {}
        });
    }
}

以最简单的方式调用小程序:

with the applet being called in its simplest way:

<applet code="com.example.applet.ExampleApplet.class" archive="${appletUrl}" width="100" height="30">
    <param name="targetPage" value="http://localhost:8080/applet/"/>
</applet><br/><br/>

其中 ${appletUrl} 返回小程序 JAR 的位置.

where ${appletUrl} returns the location of the applet JAR.

所以小程序无非是一个调用getAppletContext().showDocument(new URL(target), "_parent");来刷新当前页面的简单按钮.长期以来,这一直正确地完成其工作.现在问题来了.

So the applet is nothing more than a simple button that calls getAppletContext().showDocument(new URL(target), "_parent"); to refresh the current page. This has done its job correctly for a long time. Now here's the issue.

迁移

众所周知,Chrome 不支持 Applets.由于 IEFireFox 仍然支持它们,所以暂时搁置了一段时间.2016年底,他们也将不再支持他们.所以我们决定使用JWSJNLP来迁移applet.

As many may know, Chrome does not support Applets. Which was put aside for a while since IE and FireFox still supported them. At the end of 2016, they will also no longer support them. So we decided to migrate the applet using JWS and JNLP.

迁移这个简单的重定向按钮示例将提供以下 html 片段和 JNLP 文件:

Migrating this simple redirect button example would give the following html snippet and JNLP file:

<a href="${jnlpUrl}">Launch JNLP</a>

${jnlpUrl} 返回 JNLP 文件的位置,即:

${jnlpUrl} returns the location to the JNLP file which is:

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://localhost:8080/applet/assets/1.0-SNAPSHOT-DEV/app/assets/" href="jnlp/example.jnlp">
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.5+" initial-heap-size="32m" max-heap-size="128m" />
        <property name="jnlp.versionEnabled" value="false"/>
        <jar href="applets/ExampleApplet.jar" main="true"/>
    </resources>
    <applet-desc name="code" main-class="com.example.applet.ExampleApplet.class" width="30" height="30" >
        <param name="targetPage" value="http://localhost:8080/applet/"/>
    </applet-desc>
</jnlp>

到目前为止一切顺利,同一个小程序成功部署为 JWS 应用程序.允许从任何浏览器使用它,因为它是在它之外执行的.这也是一个问题.

So far so good, the same applet successfuly deploys as a JWS application. Allowing it to be used from any browser since it's executed outside of it. Which also is kind of the problem.

问题

getAppletContext().showDocument(new URL(target), "_parent"); 仍然进行重定向,但它使用的是 迁移文档.

The line getAppletContext().showDocument(new URL(target), "_parent"); still does a redirect, but it's using the default browser as stated in the migration documentation.

对于 AppletContext.showDocument(URL url, String target),Java Web Start 技术将忽略目标参数.

For AppletContext.showDocument(URL url, String target), the target argument will be ignored by Java Web Start technology.

与 AppletContext.showDocument 类似,Java Web Start 应用程序能够使用系统的默认 Web 浏览器通过 BasicService.showDocument API 显示 HTML 页面.

Similar to AppletContext.showDocument, Java Web Start applications are capabile of showing an HTML page using the system's default web browser by using the BasicService.showDocument API.

因此,如果我的默认浏览器是 FireFox,但我碰巧在 IE/Chrome 中浏览到这个 启用 JWS 的小程序,则会在 FireFox 中打开一个新选项卡.这是一个问题,因为我在原始浏览器中存储了信息(例如登录)!

So if my default browser is FireFox, but I happen to be browsing to this JWS-enabled applet in IE/Chrome, a new tab will be opened in FireFox. This is a problem, since I have information (e.g. login) stored in the original browser!

发现

由于应用程序在浏览器之外运行,我在考虑与原始浏览器通信的可能性时遇到问题.我不能使用 JavaScript,因为它不在浏览器中运行.我无法真正定义一种独立于系统的方式来在原始浏览器中打开选项卡.我也想到了 WebSockets,因为它们可以允许直接通信,但从我读到的内容来看,它是相当高级的并且需要一个服务器,而不是一个简单的小程序.

Since the application is running outside of the browser, I'm having issues to think of possibilities to communicate back to the original browser. I can't use JavaScript since it doesn't run within the browser. I can't really define a system-independent way to open a tab in the original browser. I've also thought of WebSockets since they could allow direct communication, but from what I've read it's pretty high-level and requires a server, not a simple applet.

当小程序打开一个新窗口时,是否有可能在原始浏览器(例如 websockets 和参数)之间进行通信或将会话从一个浏览器传递到另一个浏览器?

Is there any possibility of communicating between the original browser (e.g. websockets and parameters) or passing the session from one browser to another when the applet opens a new window?

推荐答案

我找到了一个可行的解决方案.

I have found out a working solution.

由于 applet 失去了与浏览器及其会话的所有连接,提供通信的另一种方式是使用 WebSocketsComet.就我而言,我使用 CometAtmosphere框架和 Tapestry-Atmosphere 实现,因为 Tapestry 是我正在使用的视图框架.

Since the applet loses all connection to the browser and its session, another way to provide communication is with WebSockets or Comet. In my case I used Comet with the Atmosphere framework and a Tapestry-Atmosphere implementation, since Tapestry is the view-framework I'm using.

没有深入研究 Tapestry 的实现,我做了以下事情:

Without going too deep into the Tapestry implementation, I have done the following:

  • 在客户端浏览器上设置一个Topic,它将以典型的发布/订阅方式收听广播消息.
  • Applet 提供当前浏览用户唯一的Topic 的ID,以及一个URL 以发送请求到.使用 Topic ID 作为 url 的请求参数.
  • 服务器端我有接收 Topic 作为请求参数的请求的端点.使用此参数,它会向主题发送广播(可能为空).
  • 客户端Topic 接收通知并自行执行事件.该事件是重新呈现页面的特定内容.
  • Set up a Topic on the client-side browser which will listen to broadcasted messages in a typical publish/subscribe manner.
  • Provide the Topic's ID which is unique to the current browsing user into the Applet, along with a URL to send a request to. Using the Topic ID as a request parameter for the url.
  • Server-side I have the endpoint of the request which receives the Topic as a request parameter. Using this parameter, it sends a broadcast (possibly empty) to the topic.
  • The client-side Topic receives the notification and executes an event in itself. The event being to re-render specific content of the page.

因为它使用 Comet(但也可以使用 WebSockets),所以它直接发生在浏览器中.每个浏览器实际上都订阅了该Topic,但这里只有一个.

Since it's using Comet (but could also use WebSockets) it happens directly in the browser. Every browser subscribed to that Topic actually, but here there's only one.

使之成为可能,通过来自小程序的简单请求来更新页面.小程序只有行 getAppletContext().showDocument(new URL(target), "_parent"); 更改为 new URL(target).openConnection().getInputStream();.target 是包含请求参数的 URL.

Making it in fact possible, to update the page from a simple request from the applet. The applet only had the line getAppletContext().showDocument(new URL(target), "_parent"); changed to new URL(target).openConnection().getInputStream();. With target being a URL with the included request parameter.

这篇关于如何在浏览器和 Java Web Start 小程序之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆