Reputation: 31
I have a jsp page tied to a servlet that runs groovy scripts. I am able to get to the groovy script from the servlet. But after the script runs how do I return the response from the groovy script back to the servlet to be displayed in the jsp page?
My java servlet code is as follows:
File file = new File("TestScript.groovy");
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(file);
Object[] args = {};
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
groovyObject.invokeMethod("runTest", args);
I also looked into groovyscriptengine and grovyshell but when trying to run those i get the following exception: org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: runTest
This what i have in my test script. Maybe I am not returning it properly.
class TestScript
{
@Test
public String runTest()
{
//run test
return response
}
}
Upvotes: 3
Views: 6575
Reputation: 13779
Have the runTest
groovy method return the value you want to pass to the servlet, and capture it as the return value of groovyObject.invokeMethod
:
Object ret = groovyObject.invokeMethod("runTest", args1);
System.out.println("In Java " + ret);
Upvotes: 3