title: How to communicate between browser and Java Web Start applet
body: <p><strong>Current situation</strong></p>
  <p>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:</p>
  <pre><code>public class ExampleApplet extends Applet {
      @Override
      public void init() {
          Button redirect = new Button(&quot;Redirect&quot;);
          this.add(redirect);
          final String target = this.getParameter(&quot;targetPage&quot;);
          redirect.addActionListener((ActionEvent e) -&gt; {
              try {
                  getAppletContext().showDocument(new URL(target), &quot;_parent&quot;);
              } catch (MalformedURLException ex) {}
          });
      }
  }
  </code></pre>
  <p>with the applet being called in its simplest way:</p>
  <pre><code>&lt;applet code=&quot;com.example.applet.ExampleApplet.class&quot; archive=&quot;${appletUrl}&quot; width=&quot;100&quot; height=&quot;30&quot;&gt;
      &lt;param name=&quot;targetPage&quot; value=&quot;http://localhost:8080/applet/&quot;/&gt;
  &lt;/applet&gt;&lt;br/&gt;&lt;br/&gt;
  </code></pre>
  <p>where <code>${appletUrl}</code> returns the location of the applet JAR.</p>
  <p>So the applet is nothing more than a simple button that calls <code>getAppletContext().showDocument(new URL(target), &quot;_parent&quot;);</code> to refresh the current page. This has done its job correctly for a long time. Now here's the issue.</p>
  <p><strong>Migration</strong></p>
  <p>As many may know, Chrome does not support <code>Applet</code>s. Which was put aside for a while since <code>IE</code> and <code>FireFox</code> still supported them. At the end of 2016, they will also no longer support them. So we decided to migrate the applet using <code>JWS</code> and <code>JNLP</code>.</p>
  <p>Migrating this simple redirect button example would give the following <code>html</code> snippet and <code>JNLP</code> file:</p>
  <pre><code>&lt;a href=&quot;${jnlpUrl}&quot;&gt;Launch JNLP&lt;/a&gt;
  </code></pre>
  <p><code>${jnlpUrl}</code> returns the location to the <code>JNLP</code> file which is:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;jnlp spec=&quot;1.0+&quot; codebase=&quot;http://localhost:8080/applet/assets/1.0-SNAPSHOT-DEV/app/assets/&quot; href=&quot;jnlp/example.jnlp&quot;&gt;
      &lt;security&gt;
          &lt;all-permissions/&gt;
      &lt;/security&gt;
      &lt;resources&gt;
          &lt;j2se version=&quot;1.5+&quot; initial-heap-size=&quot;32m&quot; max-heap-size=&quot;128m&quot; /&gt;
          &lt;property name=&quot;jnlp.versionEnabled&quot; value=&quot;false&quot;/&gt;
          &lt;jar href=&quot;applets/ExampleApplet.jar&quot; main=&quot;true&quot;/&gt;
      &lt;/resources&gt;
      &lt;applet-desc name=&quot;code&quot; main-class=&quot;com.example.applet.ExampleApplet.class&quot; width=&quot;30&quot; height=&quot;30&quot; &gt;
          &lt;param name=&quot;targetPage&quot; value=&quot;http://localhost:8080/applet/&quot;/&gt;
      &lt;/applet-desc&gt;
  &lt;/jnlp&gt;
  </code></pre>
  <p>So far so good, the same applet successfuly deploys as a <code>JWS</code> application. Allowing it to be used from any browser since it's executed outside of it. Which also is kind of the problem.</p>
  <p><strong>The problem</strong></p>
  <p>The line <code>getAppletContext().showDocument(new URL(target), &quot;_parent&quot;);</code> still does a redirect, but it's using the default browser as stated in the <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/applet_migration.html" rel="noreferrer">migration documentation.</a></p>
  <blockquote>
  <p>For AppletContext.showDocument(URL url, String target), the target argument will be ignored by Java Web Start technology.</p>
  <p>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.</p>
  </blockquote>
  <p>So if my default browser is FireFox, but I happen to be browsing to this <code>JWS-enabled applet</code> 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!</p>
  <p><strong>Findings</strong></p>
  <p>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 <code>JavaScript</code> 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 <code>WebSockets</code> 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.</p>
  <p>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?</p>