<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 97,  comments - 93,  trackbacks - 0

    Most developers have heard of, and possibly used, scripting languages such as Ruby, JavaScript, and Python. These dynamic languages are enjoying a resurgence in popularity, largely because of their flexibility and simplicity, and the productivity gains they promise.

    Java 6 comes with built-in support for scripting languages. You can embed scripts in various scripting languages into your Java applications, passing parameters, evaluating expressions, and retrieving results. And you can do it all pretty seamlessly.

    First of all, you obtain a new ScriptEngine object from a ScriptEngineManager, as shown here:

    ScriptEngineManager manager = new ScriptEngineManager();

    ScriptEngine engine = manager.getEngineByName("js");

    Each scripting language has its own unique identifier. The "js" here means you're dealing with JavaScript.

    Now you can start having some fun. Interacting with a script is easy and intuitive. You can assign scripting variables using the put() method and evaluate the script using the eval() method,. which returns the most recently evaluated expression processed by the script. And that pretty much covers the essentials. Here's an example that puts it all together:

    engine.put("cost", 1000);
    String decision = (String) engine.eval(
    "if ( cost >= 100){ " +
    "decision = 'Ask the boss'; " +
    "} else {" +
    "decision = 'Buy it'; " +
    "}");
    assert ("Ask the boss".equals(decision));

    You can do more than just pass variables to your scripts— you can also invoke Java classes from within your scripts. Using the importPackage() function enables you to import Java packages, as shown here:

    				
    
    engine.eval("importPackage(java.util); " +
    "today = new Date(); " +
    "print('Today is ' + today);");

    Another cool feature is the Invocable interface, which lets you invoke a function by name within a script. This lets you write libraries in scripting languages, which you can use by calling key functions from your Java application. You just pass the name of the function you want to call, an array of Objects for the parameters, and you're done! Here's an example:

    				
    
    engine.eval("function calculateInsurancePremium(age) {...}");
    Invocable invocable = (Invocable) engine;
    Object result = invocable.invokeFunction("calculateInsurancePremium",
    new Object[] {37});

    You actually can do a fair bit more than what I've shown here. For example, you can pass a Reader object to the eval() method, which makes it easy to store scripts in external files, or bind several Java objects to JavaScript variables using a Map-like Binding object. You can also compile some scripting languages to speed up processing. But you probably get the idea that the integration with Java is smooth and well thought-out.

    posted on 2007-03-07 21:49 wqwqwqwqwq 閱讀(475) 評論(0)  編輯  收藏 所屬分類: SUN Tech
    <2007年3月>
    25262728123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

    J2ME技術網站

    java技術相關

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網網絡電話,中國最優秀的網絡電話
    主站蜘蛛池模板: 国产精品久久香蕉免费播放| 久久久久亚洲精品无码系列| 亚洲国产成人精品久久| 免费VA在线观看无码| 亚洲最大免费视频网| 亚洲一区精品无码| 亚洲国产成人手机在线观看| 日韩精品内射视频免费观看| 亚洲国产精品毛片av不卡在线| 亚洲av成人一区二区三区| a毛片免费播放全部完整| 国产精品酒店视频免费看| 亚洲国产韩国一区二区| 99久久免费国产特黄| 四虎免费永久在线播放| 亚洲一区在线视频观看| 免费在线观看一级片| 亚洲中文字幕丝袜制服一区| 亚洲日韩精品国产一区二区三区| 四虎影视成人永久免费观看视频 | 成人免费无码大片a毛片软件| 亚洲成色WWW久久网站| 一级一看免费完整版毛片| 免费看无码自慰一区二区| 亚洲成人在线免费观看| 日本免费高清视频| 亚洲中文字幕第一页在线| 免费人人潮人人爽一区二区| 韩国免费三片在线视频| xxx毛茸茸的亚洲| 免费在线观看h片| 久久久久久亚洲精品| 国产一级a毛一级a看免费人娇| 免费一级大黄特色大片| 亚洲精品国产精品| 久久综合AV免费观看| 亚洲一区中文字幕在线电影网| 777爽死你无码免费看一二区 | 99久久亚洲综合精品成人网| 国产又黄又爽胸又大免费视频| 亚洲午夜无码片在线观看影院猛|