Generate JNLP dynamically
Asked Answered
J

2

11

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?

Justiciar answered 14/6, 2012 at 18:2 Comment(5)
actually it's even harder as you need to include the jnlp in the jar later (for signed jars), so the jar has to be signed dynamically tooIncalescent
Are you sure you are using the arguments in the call? Could you provide the url you are using to download the jnlp?Rivalry
I don't intend to include jnlp in the jar again, it works fine without that.Justiciar
I am trying to do the same on Struts. Can you give me an idea as to how your web.xml and struts-config.xml should look like?Cemetery
In my case, as I extend JnlpDownloadServlet class, there is a run time exception thrown which says SEVERE: Allocate exception for servlet JnlpDownloadServlet java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet. Any idea why this may happen? I am using jnlp-servlet.jar, jnlp.jar and jardiff.jar from Java Samples and Demos.Cemetery
E
8

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.

Elyn answered 15/6, 2012 at 7:27 Comment(4)
and how exactly will I read these values in <argument> tag later even if I modify my href, How should my JSP be modified in order to cater to that.Justiciar
It is still not working. Can you please tell me what should be contents of my test.jnlp posted at the server.Justiciar
I don't agree, is not the href where the arguments should be added, but the url used to download the jnlp in the first place. Example: www.example.com/app/servlet/TestServlet?arg1=blah&arg2=tahRivalry
@Andrew Thanks for the answer it helped me understand JNLP better and find a solution which works for me.Justiciar
W
1

Maybe to old for being useful but I actually patched the Sun servlet code. There is a Class JnlpFileHandler where the actual substitutions are done.... Just saying... ;-) If anyone is interested I can give you the code including a little explanation. I did not waste too much tima at it but all I can say is that I truly hope the rest of SUN's code is written in a LOT more respect to OO principles...

Wertz answered 25/4, 2013 at 15:40 Comment(1)
I recently did a mod of that code too; it works really well for my needs.Surrebuttal

© 2022 - 2024 — McMap. All rights reserved.