我想在 Google App Engine 运行时内动态评估 JavaScript 代码。
Java有这个功能,但想知道GAE是否也支持这个功能。
如果您能提供一个简单的代码将不胜感激,如果您使用它,请分享评论,谢谢。
...
GAE 支持脚本语言,但默认情况下“JavaScript”服务未注册。因此,开箱即用的 GAE 不会评估 JavaScript。
上次尝试,虽然ScriptEngine被列入白名单,但在生产环境中不可用。我必须打包犀牛罐子和我的应用程序一起。
有关 Java 脚本的一般用法的示例,您可以参考Java 文档 itself.
不过,请注意,在 GAE/J 环境中,您将需要直接调用 Rhino API。
例如,
// 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));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)