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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Harshdeep
  • 5,614
  • 10
  • 37
  • 45
  • 2
    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 too – bestsss Jun 14 '12 at 19:49
  • Are you sure you are using the arguments in the call? Could you provide the url you are using to download the jnlp? – pmoleri Jun 15 '12 at 00:04
  • 1
    I don't intend to include jnlp in the jar again, it works fine without that. – Harshdeep Jun 15 '12 at 08:05
  • 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? – deepankardixit90 May 30 '16 at 14:20
  • 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. – deepankardixit90 Jun 13 '16 at 13:36

2 Answers2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • and how exactly will I read these values in `` tag later even if I modify my `href`, How should my JSP be modified in order to cater to that. – Harshdeep Jun 15 '12 at 08:13
  • It is still not working. Can you please tell me what should be contents of my test.jnlp posted at the server. – Harshdeep Jun 15 '12 at 08:34
  • 2
    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=tah – pmoleri Jun 15 '12 at 15:34
  • @Andrew Thanks for the answer it helped me understand JNLP better and find a solution which works for me. – Harshdeep Jun 18 '12 at 07:17
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...

Lawrence
  • 1,035
  • 13
  • 8