Home / Java Patterns and Pitfalls     frequal.com

Adaptive JNLP File Via JSP

Often you want to deploy a JNLP-launched application to several different servers. However, the JNLP file needs an embedded codebase since relative codebases don't work reliably yet. Since an absolute codebase requires the server name, you need to make sure the client receives a codebase with the name of the server it is running on.

You could manually edit the codebase on each deployed machine, but there is an easier way. Use a small amount of JSP code to substitute the server name of the request in the right spot of the JSP file.

The Technique

In the web.xml for the WAR file containing your JNLP file, add this section to instruct the servlet container to process JNLP files using the JSP servlet:
  <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jnlp</url-pattern>
  </servlet-mapping>
Next, in your JNLP file, add this at the top to fetch the server name and port from the request:
<%
String host = request.getHeader("Host");
String http = request.isSecure() ? "https" : "http";
response.setContentType("application/x-java-jnlp-file");
%>
Finally, replace a hardcoded jnlp codebase attribute with one substituting the host and port from the request
  <jnlp codebase="<%=http%>://<%=host%>" ...
Now your JNLP file will always reflect the requesting URL, even if your sever is using port redirection or load balancing.
Last modified on 14 Sep 2012 by AO

Copyright © 2024 Andrew Oliver