【问题标题】:Does Google App Engine support Java Script Engine? [closed]Google App Engine 是否支持 Java Script Engine? [关闭]
【发布时间】:2013-11-18 09:18:43
【问题描述】:

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

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

如果你能提供一个简单的代码将非常感激,如果你使用它,请分享一下cmet,谢谢。

...

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

【问题讨论】:

    标签: java javascript google-app-engine dynamic


    【解决方案1】:

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

    有关Java脚本的一般用法示例,您可以参考Java documentation本身。

    但请注意,在 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));
    

    【讨论】:

    • 即使用jar也不行。所以你没有运行它,对吧?
    • 我当然在运行它 :) ...我现在不在我的个人机器旁。回来后我会分享所需的代码。
    • 对最初的失误感到抱歉。仅添加 jar 不起作用,您需要直接使用 Rhino API。请查看原始答案以获取有关如何调用脚本的信息。
    【解决方案2】:

    https://developers.google.com/appengine/docs/java/jrewhitelist 在其列入白名单(允许)的 API 中包含 javax.script.ScriptEngine,所以,是的。

    【讨论】:

    • 对,但是犀牛服务必须注册,我找不到实现的方法。
    • 虽然类被列入白名单,但它们的功能是有限的。在您的本地机器上,“ScriptEngineManager.getEngineFactories()”将返回几个不同的引擎。在 App Engine 上,它什么也不返回。许多代码示例使用“ScriptEngineManager.getEngineByName("JavaScript")”,但在 App Engine 上,您只会返回 null。我建议改用 Rhino。
    猜你喜欢
    • 1970-01-01
    • 2014-11-27
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2020-02-21
    相关资源
    最近更新 更多