vineri, 24 ianuarie 2014

Java Web Start - using parameters in URL for the application started via Web Start

A few days ago, I have been faced with the problem of having to pass some parameters (via a URL using a GET request) to a Java Web Start based application, something I haven't done in the past :)

This is actually quite simple, what you basically  have to do is to generate the JNLP file dynamically instead of using a static one.   There are several ways of generating a JNLP file dynamically, such as using a JSP file, servlet, SOA oriented service etc.

I will shortly present a way to generate this JNLP file by using a JSP.  I am assuming you already have a .jnlp file, and you want to adapt it to use parameters.

1. Rename the existing .jnlp file to jsp.

2. Adding the following directives at the beginning of the jnlp, now jsp file (will be used to compose the URL and will set the response headers):

<%
String     urlSt = request.getRequestURL().toString();    
String     jnlpCodeBase=urlSt.substring(0,urlSt.lastIndexOf('/'));    
String     jnlpRefURL=urlSt.substring(urlSt.lastIndexOf('/')+1,urlSt.length());    
%>
<%
     response.setContentType("application/x-java-jnlp-file");  
     response.setHeader("Cache-Control", null);
     response.setHeader("Set-Cookie", null);
     response.setHeader("Vary", null);
%>

3.  Customize the <jnlp> tag codebase param to include your parameters.  As you can see, I am using two example parameters :
   <jnlp codebase="<%=request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/" %>" href="jnlp/launch.jnlp?url=<%=request.getParameter("url")%>&user=<%=request.getParameter("user")%>">


4. Add your arguments again to the application description tag inside your JNLP file (now JSP):
<application-desc main-class="com.ibm.myapp.SomeGreatApp">
       <argument><%=request.getParameter("url")%></argument>
       <argument><%=request.getParameter("user")%></argument>
    </application-desc>

5. Deploy your app, then you can access the JNLP file using a request such as:
http://localhost:8080/mayapp/jnlp/launch.jsp?url=http://localhost:8080/someURL/somepage.html&user=some_username

That way, the params you send using the GET request, will be passed to the application started via Web Start.

Niciun comentariu:

Trimiteți un comentariu