转自:http://coffeef.iteye.com/blog/492487
IBM有详细介绍:http://www.ibm.com/developerworks/cn/java/j-javascripting1/
JDK1.6加入了对Script(JSR223)的支持。这是一个脚本框架,提供了让脚本语言来访问Java内部的方法。你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本。这个脚本API允许你为脚本语言提供Java支持。另外,Web Scripting Framework允许脚本代码在任何的Servlet容器(例如Tomcat)中生成Web内容。
关于ScriptEngine和ScriptEngineManager的使用:
超做js首先需要ScriptEngine
在jdk中可以用3中方式获得:
- mgr.getEngineByExtension(String extension)
- mgr.getEngineByMimeType(String mimeType)
- mgr.getEngineByName(String shortName)
mgr.getEngineByExtension(String extension) mgr.getEngineByMimeType(String mimeType) mgr.getEngineByName(String shortName)
getEngineByExtension可用参数:
- js
js
eg:
- ScriptEngineManager mgr = new ScriptEngineManager();
- ScriptEngine engine = mgr.getEngineByExtension("js" );
ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByExtension("js");
getEngineByMimeType可用参数:
- application/javascript
- application/ecmascript
- text/javascript
- text/ecmascript
application/javascript application/ecmascript text/javascript text/ecmascript
eg:
- ScriptEngineManager mgr = new ScriptEngineManager();
- ScriptEngine engine = mgr.getEngineByMimeType("application/javascript" );
ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByMimeType("application/javascript");
getEngineByName可用参数:
- js
- rhino
- JavaScript
- javascript
- ECMAScript//js和javascript是它的扩展
js rhino JavaScript javascript ECMAScript//js和javascript是它的扩展
eg:
写道
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
ScriptEngine engine = mgr.getEngineByName("JavaScript");
具体解析js可以是加载js文件或者js 脚本字符串:
例如夹在js文件:
- engine.eval( new FileReader(jsfile));
engine.eval(new FileReader(jsfile));
或者
- engine.eval(jsstring);
engine.eval(jsstring);
解析js需要Invocable 类:首先ScriptEngine 记载js文件或者字符串(如上):
然后转化为Invocable 就可以用来执行我们的js函数:
eg:
- engine.eval( "function test(){return 'this is test js in java'}" );
- Invocable inv = (Invocable) engine;
- String value = String.valueOf(inv.invokeFunction("test" ));
engine.eval("function test(){return 'this is test js in java'}"); Invocable inv = (Invocable) engine; String value = String.valueOf(inv.invokeFunction("test"));
以下测试 部分来自:http://sharong.iteye.com/blog/474476,看有人写了就转载过来了。感谢sharong!
以下是测试:
- /**
- * 演示脚本语言如何使用JDK平台下的类,调用一个JDK平台的Swing窗口
- *
- * @param engine ScriptEngine实例
- * @return void
- * */
- private static void testUsingJDKClasses(ScriptEngine engine) throws Exception {
- // Packages是脚本语言里的一个全局变量,专用于访问JDK的package
- String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}" ;
- engine.eval(js);
- // Invocable 接口: 允许java平台调用脚本程序中的函数或方法
- Invocable inv = (Invocable) engine;
- // invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数;
- inv.invokeFunction( "doSwing" , "Scripting Swing" );
- }
- /**
- * 演示脚本语言如何使用JDK平台下的类,调用一个JDK平台的Swing窗口
- *
- * @param engine ScriptEngine实例
- * @return void
- * */
- private static void testUsingJDKClasses(ScriptEngine engine) throws Exception {
- // Packages是脚本语言里的一个全局变量,专用于访问JDK的package
- String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}" ;
- engine.eval(js);
- // Invocable 接口: 允许java平台调用脚本程序中的函数或方法
- Invocable inv = (Invocable) engine;
- // invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数;
- inv.invokeFunction("doSwing" , "Scripting Swing" );
- }
/** * 演示脚本语言如何使用JDK平台下的类,调用一个JDK平台的Swing窗口 * * @param engine ScriptEngine实例 * @return void * */ private static void testUsingJDKClasses(ScriptEngine engine) throws Exception { // Packages是脚本语言里的一个全局变量,专用于访问JDK的package String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}"; engine.eval(js); // Invocable 接口: 允许java平台调用脚本程序中的函数或方法 Invocable inv = (Invocable) engine; // invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数; inv.invokeFunction("doSwing", "Scripting Swing"); }
下面的代码段演示脚本语言如何实现Java的接口
- /**
- * 演示脚本语言如何实现Java的接口,启动线程来运行script提供的方法
- *
- * @param engine ScriptEngine实例
- * @return void
- * */
- private static void testScriptInterface(ScriptEngine engine) throws ScriptException {
- String script = "var obj = new Object(); obj.run = function() { println('run method called'); }" ;
- engine.eval(script);
- Object obj = engine.get( "obj" );
- Invocable inv = (Invocable) engine;
- Runnable r = inv.getInterface(obj, Runnable. class );
- Thread th = new Thread(r);
- th.start();
- }
- /**
- * 演示脚本语言如何实现Java的接口,启动线程来运行script提供的方法
- *
- * @param engine ScriptEngine实例
- * @return void
- * */
- private static void testScriptInterface(ScriptEngine engine) throws ScriptException {
- String script = "var obj = new Object(); obj.run = function() { println('run method called'); }" ;
- engine.eval(script);
- Object obj = engine.get("obj" );
- Invocable inv = (Invocable) engine;
- Runnable r = inv.getInterface(obj, Runnable.class );
- Thread th = new Thread(r);
- th.start();
- }
/** * 演示脚本语言如何实现Java的接口,启动线程来运行script提供的方法 * * @param engine ScriptEngine实例 * @return void * */ private static void testScriptInterface(ScriptEngine engine) throws ScriptException { String script = "var obj = new Object(); obj.run = function() { println('run method called'); }"; engine.eval(script); Object obj = engine.get("obj"); Invocable inv = (Invocable) engine; Runnable r = inv.getInterface(obj, Runnable.class); Thread th = new Thread(r); th.start(); }
演示如何在Java中调用脚本语言的方法
- /**
- * 演示如何在Java中调用脚本语言的方法,通过JDK平台给script的方法中的形参赋值
- *
- * @param engine ScriptEngine实例
- * @return void
- * */
- private static void testInvokeScriptMethod(ScriptEngine engine) throws Exception {
- String script = "function helloFunction(name) { return 'Hello everybody,' + name;}" ;
- engine.eval(script);
- Invocable inv = (Invocable) engine;
- String res = (String) inv.invokeFunction( "helloFunction" , "Scripting" );
- System.out.println( "res:" + res);
- }
- /**
- * 演示如何在Java中调用脚本语言的方法,通过JDK平台给script的方法中的形参赋值
- *
- * @param engine ScriptEngine实例
- * @return void
- * */
- private static void testInvokeScriptMethod(ScriptEngine engine) throws Exception {
- String script = "function helloFunction(name) { return 'Hello everybody,' + name;}" ;
- engine.eval(script);
- Invocable inv = (Invocable) engine;
- String res = (String) inv.invokeFunction("helloFunction" , "Scripting" );
- System.out.println("res:" + res);
- }
/** * 演示如何在Java中调用脚本语言的方法,通过JDK平台给script的方法中的形参赋值 * * @param engine ScriptEngine实例 * @return void * */ private static void testInvokeScriptMethod(ScriptEngine engine) throws Exception { String script = "function helloFunction(name) { return 'Hello everybody,' + name;}"; engine.eval(script); Invocable inv = (Invocable) engine; String res = (String) inv.invokeFunction("helloFunction", "Scripting"); System.out.println("res:" + res); }
演示如何暴露Java对象为脚本语言的全局变量
- /**
- * 演示如何暴露Java对象为脚本语言的全局变量,同时演示将File实例赋给脚本语言,并通过脚本语言取得它的各种属性值
- *
- * @param engine ScriptEngine实例
- * @return void
- * */
- private static void testScriptVariables(ScriptEngine engine) throws ScriptException {
- File file = new File( "test.txt" );
- engine.put( "f" , file);
- engine.eval( "println('Total Space:'+f.getTotalSpace())" );
- engine.eval( "println('Path:'+f.getPath())" );
- }