Google App Engine是否支持Java Script Engine? [英] Does Google App Engine support Java Script Engine?

查看:288
本文介绍了Google App Engine是否支持Java Script Engine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在Google App Engine运行时内动态评估JavaScript代码。



Java具有此功能,但想知道GAE是否支持此功能。 >

如果可以提供一个简单的代码将非常欣赏,如果你使用它,请分享关于它的意见,谢谢。



...



GAE支持脚本语言,但默认情况下,JavaScript服务未注册。所以,GAE开箱即用不会评估JavaScript。

解决方案

上次我试过,尽管ScriptEngine被列入白名单在生产环境中不可用。我必须与我的应用程序一起打包 Rhino.jar



有关Java中脚本编写的一般用法的示例,可以参考 Java文档本身。



尽管请注意,在GAE / J环境中,您将需要直接调用Rhino API。



例如,

  /导入语句。 
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

private Object executeUsingRhino(String script)throws异常
{
上下文ctx = Context.enter();
try
{
Scriptable scope = ctx.initStandardObjects();
return ctx.evaluateString(scope,script,< cmd>,1,null);
}
finally
{
Context.exit();
}
}


//调用使用以下代码片段返回字符串输出的脚本代码片段
String output = Context.toString(executeUsingRhino脚本));


I want evaluate dynamically JavaScript code inside the Google App Engine runtime.

Java have this feature but Want to know if this is supported by GAE too.

If you can provide a simple code will be very appreciate, and if you use it, please shares comments about it, thanks.

...

GAE support Scripting Languages but by default 'JavaScript' service is not register. So GAE out-of-the-box do not evaluate JavaScript.

解决方案

Last time I tried, though ScriptEngine is whitelisted, it is not available in the production environment. I had to package the Rhino.jar along with my app.

For examples on general usage of scripting in Java, you can refer to the Java documentation itself.

Though, note that in the GAE/J environment you will need to invoke the Rhino APIs directly.

For example,

// Import statements.
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

private Object executeUsingRhino(String script) throws Exception
{
    Context ctx = Context.enter();
    try
    {
        Scriptable scope = ctx.initStandardObjects();
        return ctx.evaluateString(scope, script, "<cmd>", 1, null);
    }
    finally
    {
        Context.exit();
    }
}


// Invoke a script that returns a string output using the following code snippet
String output = Context.toString(executeUsingRhino(script));

这篇关于Google App Engine是否支持Java Script Engine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆