How to interact between browser and Java Web Start applet

Current situation

We are currently using the applet to perform some operations, after which it redirects the current page. At its core, you could see the applet as follows:

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) {} }); } } 

when the applet is called in the 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 JAR applet.

So, an 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 is the question.

Migration

As many people know, Chrome does not support Applet s. Some time was delayed since IE and FireFox still supported them. At the end of 2016, they will also no longer support them. Therefore, we decided to port the applet using JWS and JNLP .

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

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

${jnlpUrl} returns the location in the JNLP file, which:

 <?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 is successfully deployed as a JWS application. Allow it to be used from any browser, as it runs outside of it. Which is also a problem.

Problem

String getAppletContext().showDocument(new URL(target), "_parent"); still redirects, but uses the default browser as indicated in the migration documentation .

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

Like AppletContext.showDocument, Java Web Start applications can use the HTML page using the default web browser using the BasicService.showDocument API.

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

conclusions

Since the application runs outside the browser, I am having problems communicating with the original browser. I can not use JavaScript , because it does not run in the browser. I cannot determine a system-independent way to open a tab in the source browser. I also thought about WebSockets , as they can allow direct communication, but from what I read it is pretty high-level and requires a server, not a simple applet.

Is there a possibility of communication between the original browser (for example, websockets and parameters) or the transfer of a session from one browser to another when the applet opens a new window?

+7
java browser java-web-start applet jnlp
source share
2 answers

I found a working solution.

Since the applet loses all connection to the browser and its session, another way is to connect to WebSockets or Comet . In my case, I used Comet with Atmosphere and Tapestry-Atmosphere , since Tapestry is the presentation structure I use.

Without delving into the implementation of Tapestry , I did the following:

  • Configure Topic in a client browser that will listen to broadcast messages in the usual publish/subscribe .
  • Provide a Topic identifier that is unique to the current user to view in the applet , as well as the URL to submit the request. Use Topic identifier as request parameter for URL.
  • On the server side, I have a request endpoint that receives Topic as a request parameter. Using this parameter, it sends a broadcast (possibly empty) topic.
  • Topic client side receives a notification and executes the event itself. The event is to redisplay specific page content.

Since it uses Comet (but can also use WebSockets ), this happens directly in the browser. Each browser has subscribed to this Topic in fact, but there is only one.

Make it virtually possible to refresh the page from a simple request from the applet. The applet only had the string getAppletContext().showDocument(new URL(target), "_parent"); changed to new URL(target).openConnection().getInputStream(); . If target is a URL with the request parameter enabled.

+5
source share

This is a response to the old thread. We are faced with a similar situation in our environment. Here's how it was reviewed.

Can an applet that uses JavaScript to communicate with a web server migrate to JWS?

The solution was something similar, as described by James. We saved the request hash map on the server using uid as the key. After that, the web application saves a lengthy survey of the server to check the status of the JWS; and JWS sends the status to the server.

We did not use third-party components; it was a simple URL link.

+1
source share

All Articles