0
JavaScriptでJavascriptを実行するためにRhino JSを使用していますが、JavaクラスからJavaクラスにアクセスする方法はありますか?Rhinoを使用してJavascriptからJavaクラス(および関数)にアクセスする方法JS
public void execute(Request request, Response response){
String script = "function abc(x,y) {return x+y;}"; // example how to access the request and response object from within the script?
Context context = Context.enter();
try {
ScriptableObject scope = context.initStandardObjects();
Scriptable that = context.newObject(scope);
Function fct = context.compileFunction(scope, script, "script", 1, null);
Object result = fct.call(context, scope, that, new Object[] { 2, 3 });
System.out.println(Context.jsToJava(result, int.class));
}
finally {
Context.exit();
}
}
上記のコード例は非常に単純ですが、スクリプト内から要求と応答オブジェクトにアクセスする方法が考えられます。出来ますか?
例:
function abc(request,response) {
var body = request.body;
response.body = body;
return response;
}