<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 閱讀(469) 評論(0)  編輯  收藏 所屬分類: SUN Tech
    <2007年3月>
    25262728123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

    J2ME技術網站

    java技術相關

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網網絡電話,中國最優秀的網絡電話
    主站蜘蛛池模板: 亚洲欧美日韩中文高清www777| 亚洲中文无码av永久| 国产AV无码专区亚洲AV琪琪| 女人张开腿等男人桶免费视频| 精品亚洲AV无码一区二区三区| 两性刺激生活片免费视频| 亚洲国产日韩女人aaaaaa毛片在线| 久视频精品免费观看99| 亚洲黄色三级网站| 久久久久久精品免费看SSS| 亚洲一级高清在线中文字幕| 午夜影视在线免费观看| 亚洲a∨无码一区二区| 亚洲国产成人久久综合一区77| 男女猛烈无遮掩视频免费软件| 亚洲最大黄色网址| 国产va免费精品观看精品| jiz zz在亚洲| 免费成人午夜视频| 成人无码区免费A∨直播| 亚洲国语精品自产拍在线观看| 亚洲熟妇av午夜无码不卡| 国产成人精品免费直播 | 亚洲色WWW成人永久网址| 久久精品国产这里是免费| 91亚洲国产成人精品下载| 女人让男人免费桶爽30分钟| 朝桐光亚洲专区在线中文字幕| 伊人久久精品亚洲午夜| 久久久久久AV无码免费网站| avtt天堂网手机版亚洲| 国产福利免费在线观看| 日韩免费在线视频| 亚洲小说图区综合在线| 国产日韩成人亚洲丁香婷婷| 97在线视频免费播放| 亚洲av午夜电影在线观看| 亚洲乱码精品久久久久..| 四虎在线成人免费网站| 久久亚洲色WWW成人欧美| 亚洲av最新在线网址|