Generate JNLP dynamically

Harshdeep picture Harshdeep · Jun 14, 2012 · Viewed 11.9k times · Source

I need to pass argument to JNLP dynamically for which I tried using a servlet which extends JnlpDownloadServlet and then includes a jsp which has all the JNLP XML written into it.

But when I invoke the downloaded JNLP I get BadFieldException.

Servlet

public class TestServlet extends JnlpDownloadServlet {  
public void service(ServletRequest req, ServletResponse res)  throws ServletException, IOException {  
HttpServletRequest request = (HttpServletRequest) req;
res.setContentType("application/x-java-jnlp-file");
request.getRequestDispatcher("/jnlp.jsp").include(request, res);  
}  

jnlp.jsp

Used for dumping dynamic JNLP:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase=<%=request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/" %> href="test.jnlp">
  <information>
   <title>Demo</title>
   <vendor>Sun Microsystems, Inc.</vendor>
  </information>
  <security>
   <all-permissions/>
  </security>
  <resources>
   <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
   <jar href="lib/test.jar" main="true" />
  </resources>
  <application-desc name="Dynamic Tree Demo Application" main-class="org.Test" width="300"   height="300">
       <argument><%=request.getParameter("arg1")%></argument>  
       <argument><%=request.getParameter("arg2")%></argument>
  </application-desc>
  <update check="background"/>
</jnlp>

I cannot see the request parameters being received correctly in downloaded JNLP but the above request.getScheme and request.getServerName seem to be working fine. Because of argument value not being received correctly I get BadFieldException when JNLP tries to execute.

How to solve this?

Answer

Andrew Thompson picture Andrew Thompson · Jun 15, 2012

Logically, href="test.jnlp" should be something like href="test.jnlp?arg1=blah&arg2=tah".

AFAIU the JWS client will reach back to the server using the exact coodebase/href stated in the JNLP.

Also, definitely listen to what bestsss has to say.