Search code examples
javascriptjspjava-web-start

Incorrectly encoded URL in jnlp file when called from javascript


I'm trying to start a Java Web Start application with Javascript, whereas the Javascript code sends a parameter containing a url (window.location.origin) to the jnlp file. My problem is that in the jnlp file the url is incorrectly encoded, i.e. instead of writing http://localhost:7001/root to the file, I get http\x3A\x2F\x2Flocalhost\x3A7001/root and thus can't start my application.

I'm calling the Java Web Start application from a jsp file with javascript (index.jsp):

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript">

      window.onload = function() {
        window.open('/root/my.jsp?hostname=' +window.location.origin+ '&date=' + Date.now());
      }
  </head>
  <body>
  </body>
</html>

My jnlp file (webStart.jsp):

<%@ page contentType="application/x-java-jnlp-file"%>
<?xml version="1.0" encoding="UTF-8"?>
  <jnlp spec="1.5" codebase="${param.hostname}/root/?v=${param.date}">
  <information>
    <title>title</title>
    <vendor>vendor</vendor>
  </information>
  <security>
    <all-permissions/>
  </security>
  <resources>
    <j2se version="1.7+"/>
    <jar href="my_application.jar"/>
  </resources>
  <applet-desc main-class="myPackage.main" name="my-applet" height="1" width ="1"/>
</jnlp>

My application server is Glassfish 3.1. I tried changing the UTF-8 and ISO-8859-1 encodings, but always get the same result. What else could be the solution to this problem?

Edit: I also tried encodeURIComponent(window.location.origin), but this didn't change anything.


Solution

  • Building the URL with window.location.protocol, window.location.hostname and window.location.port didn't handle dashes in my URL, so a better solution is to use Java Code to decode the URL.

    My webStart.jsp file looks then as following:

    <%@page import="java.net.URLDecoder"%>
    <%@ page contentType="application/x-java-jnlp-file"%>
    <% String hostname = URLDecoder.decode(request.getParameter("hostname").replace("\\x", "%"), "UTF-8"); %>
    <?xml version="1.0" encoding="UTF-8"?>
    <jnlp spec="1.5" codebase="<%=hostname%>/root/?v=${param.date}">
      <information>
        <title>title</title>
        <vendor>vendor</vendor>
      </information>
      <security>
        <all-permissions/>
      </security>
      <resources>
        <j2se version="1.7+"/>
        <jar href="my_application.jar"/>
      </resources>
      <applet-desc main-class="myPackage.main" name="my-applet" height="1" width ="1"/>
    </jnlp>