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

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

问题描述

当前情况



我们目前使用applet执行某些操作,然后重定向当前页面。在其核心中,您可以看到applet如下:

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

以最简单的方式调用applet:

 < applet code =com.example.applet.ExampleApplet.classarchive =$ {appletUrl}width = 100身高=30> 
< param name =targetPagevalue =http:// localhost:8080 / applet //>
< / applet>< br />< br />

其中 $ {appletUrl} 返回位置applet JAR。



所以applet只不过是一个简单的按钮,它调用 getAppletContext()。showDocument(new URL(target), _parent); 刷新当前页面。这已经很长时间正确地完成了它的工作。现在问题就出现了。



迁移



众所周知,Chrome确实如此不支持 Applet s。由于 IE FireFox 仍支持它们,因此搁置一段时间。在2016年底,他们也将不再支持他们。所以我们决定使用 JWS JNLP 来迁移applet。



迁移此简单重定向按钮示例将提供以下 html 代码段和 JNLP 文件:

 < a href =$ {jnlpUrl}>启动JNLP< / a> 

$ {jnlpUrl} 将位置返回 JNLP 文件是:

 <?xml version = 1.0encoding =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 =32mmax-heap-size =128m/>
< property name =jnlp.versionEnabledvalue =false/>
< jar href =applets / ExampleApplet.jarmain =true/>
< / resources>
< applet-desc name =codemain-class =com.example.applet.ExampleApplet.classwidth =30height =30>
< param name =targetPagevalue =http:// localhost:8080 / applet //>
< / applet-desc>
< / jnlp>

到目前为止,同样的applet成功部署为 JWS 申请。允许从任何浏览器使用它,因为它在它之外执行。这也是一个问题。



问题



getAppletContext()。showDocument(新URL(目标),_ parent); 仍然进行重定向,但是它使用迁移文档。


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



类似对于AppletContext.showDocument,Java Web Start应用程序可以使用BasicService.showDocument API使用系统的默认Web浏览器显示HTML页面。


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



结果



由于应用程序在浏览器之外运行,因此我在考虑与原始浏览器进行通信的可能性时遇到问题。我不能使用 JavaScript ,因为它不在浏览器中运行。我无法真正定义在原始浏览器中打开选项卡的独立于系统的方法。我还想到了 WebSockets ,因为它们可以允许直接通信,但是从我所读到的它是非常高级的并且需要服务器,而不是简单的applet。 / p>

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

解决方案

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



applet 失去与浏览器及其会话的所有连接,另一种提供通信的方式是 WebSockets Comet 。在我的情况下,我使用 Comet Atmosphere 框架和 Tapestry-Atmosphere 实现,因为 Tapestry 是我正在使用的视图框架。



如果不深入 Tapestry 实现,我已经完成了以下操作:




  • 在客户端浏览器上设置一个主题,它将以典型的发布/订阅收听广播的消息方式。

  • 提供主题的I​​D,该ID对于当前浏览用户而言是唯一的 Applet ,以及发送请求的 URL 。使用主题 ID作为URL的请求参数。

  • 服务器端我有接收<的请求的端点code>主题作为请求参数。使用此参数,它会向主题发送广播(可能为空)。

  • 客户端主题接收通知并执行一个事件本身。该事件将重新呈现页面的特定内容。



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



实际上可以实现更新来自applet的简单请求的页面。 applet只有行 getAppletContext()。showDocument(new URL(target),_ parent); 更改为新URL(目标)。 。的openConnection()的getInputStream(); 。使用 target 作为包含请求参数的URL。


Current situation

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/>

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

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.

Migration

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.

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

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

${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>

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.

The problem

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.

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

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.

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!

Findings

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.

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.

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.

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

  • 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.

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.

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 applet之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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