WebStart JNLP参数传递

Java 专栏收录该内容
37 篇文章 1 订阅

通过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);
	}
	
	

}



  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:博客之星2020 设计师:CSDN官方博客 返回首页

打赏作者

xcbeyond

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值