WebStart JNLP参数传递

作者:xcbeyond
疯狂源自梦想,技术成就辉煌!微信公众号:《程序猿技术大咖》号主,专注后端开发多年,拥有丰富的研发经验,乐于技术输出、分享,现阶段从事微服务架构项目的研发工作,涉及架构设计、技术选型、业务研发等工作。对于Java、微服务、数据库、Docker有深入了解,并有大量的调优经验。

通过WebStart 启动打开JNLP文件,虽说可以通过url在浏览器中直接打开,但其中参数的传递该如何弄呢?

有两种方法:
1.修改WebStart源码

   需修改jnlp.sample.servlet.JnlpFileHandler.java中的specializeJnlpTemplate 方法

原方法为:

    private String specializeJnlpTemplate( HttpServletRequest request, String respath, String jnlpTemplate, Stack<Long> timeStack ) throws ErrorResponseException
        {
            System.out.println("JnlpFileHandler.specializeJnlpTemplate() jnlpTemplate:" + jnlpTemplate);
            System.out.println("request.getParameter(\"ticket\"):" + request.getParameter("ticket"));
            String urlprefix = getUrlPrefix( request );
            int idx = respath.lastIndexOf( '/' ); //
            String name = respath.substring( idx + 1 );    // Exclude /
            String codebase = respath.substring( 0, idx + 1 ); // Include /
            jnlpTemplate = substitute( jnlpTemplate, "$$name", name );
            jnlpTemplate = substitute( jnlpTemplate, "$$codebase", urlprefix + request.getContextPath() + codebase );
            jnlpTemplate = substitute( jnlpTemplate, "$$hostname", request.getServerName() );
            jnlpTemplate = substitute( jnlpTemplate, "$$context", urlprefix + request.getContextPath() );
            jnlpTemplate = substitute( jnlpTemplate, "$$site", urlprefix );
            
            ……
            return jnlpTemplate;
        }


如传递ticket参数,修改后:

    private String specializeJnlpTemplate( HttpServletRequest request, String respath, String jnlpTemplate, Stack<Long> timeStack ) throws ErrorResponseException
        {
            System.out.println("JnlpFileHandler.specializeJnlpTemplate() jnlpTemplate:" + jnlpTemplate);
            System.out.println("request.getParameter(\"ticket\"):" + request.getParameter("ticket"));
            String urlprefix = getUrlPrefix( request );
            int idx = respath.lastIndexOf( '/' ); //
            String name = respath.substring( idx + 1 );    // Exclude /
            String codebase = respath.substring( 0, idx + 1 ); // Include /
            jnlpTemplate = substitute( jnlpTemplate, "$$name", name );
            jnlpTemplate = substitute( jnlpTemplate, "$$codebase", urlprefix + request.getContextPath() + codebase );
            jnlpTemplate = substitute( jnlpTemplate, "$$hostname", request.getServerName() );
            jnlpTemplate = substitute( jnlpTemplate, "$$context", urlprefix + request.getContextPath() );
            jnlpTemplate = substitute( jnlpTemplate, "$$site", urlprefix );
            if(null != request.getParameter("ticket") && !"".equals(request.getParameter("ticket"))) {
                jnlpTemplate = substitute( jnlpTemplate, "$$ticket", request.getParameter("ticket") );
                System.out.println("request ticket:" + request.getParameter("ticket"));
            }
            
           ……
            return jnlpTemplate;
        }







2.使用Sevlet或jsp文件方式动态生成JNLP文件,然后直接输出到浏览器。

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.charset.Charset;
    import java.util.ArrayList;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    /**
     * 生成JNLP文件内容
     * @author xcbeyond
     * 2017-7-20上午09:19:06
     */
    public class PrintJnlpFileServlet extends HttpServlet{
        private static final long serialVersionUID = 2476758371307727115L;
        
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse resp)
                throws ServletException, IOException {
        //设置文件编码格式为utf-8
        resp.setCharacterEncoding("utf-8");
        //禁止浏览器和webstart缓存jnlp内容
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Expires", "0");
        //给定动态文件名
        resp.setHeader("Content-Disposition", "filename=\"dynamic.jnlp\";");
        //设置文件类型为jnlp文件类型
        resp.setContentType("application/x-java-jnlp-file");
            
            PrintWriter pw = resp.getWriter();
            pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            pw.println("<jnlp spec=\"1.6+\" codebase=\"http://localhost:8080/\" href=\"\">");
            pw.println("    <information>");
            ……
            
            pw.println("    <application-desc main-class=\"org.apache.pivot.wtk.DesktopApplicationContext\">");
            ……
            pw.println("        <argument>--ticket=" + request.getParameter("ticket") + "</argument>");
            pw.println("    </application-desc>");
            pw.println("</jnlp>");
            
       
            pw.flush();
            pw.close();
        }
     
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            super.doPost(req, resp);
        }
        
        
     
    }