简体   繁体   中英

Dynamic JNLP using JSP

I have a JSP file which returns a JNLP file when called through a hyperlink. But it downloads as a jsp (xxx.jsp instead of xxx.jnlp) file though the content is the expected jnlp.

Following is my JSP

    <%    
      response.setHeader("Pragma", "no-cache");
      response.setHeader("Expires", "0");
      response.setContentType("application/x-java-jnlp-file");                            
      response.setHeader("Cache-Control", null);
      response.setHeader("Set-Cookie", null);
      response.setHeader("Vary", null);

      // An installer must reply with the version number for a given install
      if (response.containsHeader("x-java-jnlp-version-id"))
        response.setHeader("x-java-jnlp-version-id", "WASClient6.1.0");               
      else
        response.addHeader("x-java-jnlp-version-id", "WASClient6.1.0");
      String codeBase = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/";
%>

<?xml version="1.0" encoding="utf-8"?> 
<jnlp spec="1.0+" codebase="<%=codeBase %>">
        <information>
                <title>XXX</title>
        </information>
        <security>
                <all-permissions/>
        </security>
        <resources>

        </resources>
        <application-desc main-class="TestApp">
        </application-desc>
</jnlp>

Following is the resulting jsp

 <?xml version="1.0" encoding="utf-8"?> 
<jnlp spec="1.0+" codebase="<%=codeBase %>">
        <information>
                <title>XXX</title>
        </information>
        <security>
                <all-permissions/>
        </security>
        <resources>

        </resources>
        <application-desc main-class="TestApp">
        </application-desc>
</jnlp>

Any idea what I did wrong? Thanks.

You really shouldn't be using scriptlets, but if you need to, you should generate the jnlp file from the scriptlets and then do a response.redirect to the created jnlp file

 ServletContext servletContext = session.getServletContext();
 String localDir = servletContext.getRealPath("").replace('\\', '/');
 File jnlp = new File(localDir + "something.jnlp");
 FileWriter fw = new FileWriter(jnlp);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(//contents of jnlp file);
 response.sendRedirect(jnlp.getName());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM