使用 JSP 的动态 JNLP

Dynamic JNLP using JSP

我有一个 JSP 文件,当通过超链接调用时,该文件 returns 是一个 JNLP 文件。但它下载为 jsp(xxx.jsp 而不是 xxx.jnlp)文件,尽管内容是预期的 jnlp。

以下是我的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>

结果如下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>

知道我做错了什么吗? 谢谢

您确实不应该使用 scriptlet,但如果需要,您应该从 scriptlet 生成 jnlp 文件,然后对创建的 jnlp 文件执行 response.redirect

 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());