On 18.10.2019 13:32, Rony G. Flatscher (Apache) wrote:
> Thank you very much for your remarks and pointers, Mark!
>
> ---rony
>
> P.S.: Not sure as of yet, what scope it should be. The idea would be to allow 
> any of the Java script
> languages to be used. Currently the naive assumption is that one could 
> redirect the script's stdout
> to the servlet's 'out' object (by using ScriptContext.setWriter(out)) and 
> using ScriptEngineFactory
> helper methods getOutputStatement(...), getProgram(...) and maybe 
> getMethodCallSyntax(...) for
> creating the appropriate script program from the JSP that then gets 
> dispatched via
> ScriptEngine.eval(...) from the created Java program.

... cut ...

Just to give an idea what the Java code employing javax.script might look like 
(untested), assuming
that from the JSP the respective script program got created (referred to by 
"String
scriptFromJSP=..." in the Java snippet to be generated below):

    import javax.script.*;

    ... cut ...

                // the scriptCode value is created when transforming the JSP to 
its script code
    representation using
                // ScriptEngineFactory utility methods
            String scriptFromJSP="...script created from JSP ...";
            try
            {
                    // create a ScriptContext for this run
                ScriptContext sc=new SimpleScriptContext();
                sc.setWriter(out);      // set stdout to 'out'

                    // get its ENGINE_SCOPE Bindings
                Bindings     
engineScope=sc.getBindings(ScriptContext.ENGINE_SCOPE);
                    // set JSP file name for the script
                engineScope.put(ScriptEngine.FILENAME, jspFileName);
                    // define arguments for the script program
                engineScope.put(ScriptEngine.ARGV, new 
Object[]{request,response});

                    // set JSP objects
                engineScope.put("application", application);
                engineScope.put("config", config);
                engineScope.put("out", out);
                engineScope.put("pageContext", pageContext);
                engineScope.put("request" , request);
                engineScope.put("response", response);

                    // get ScriptEngine, evaluate (run) script program
                ScriptEngineManager sem=new ScriptEngineManager();
                ScriptEngine se=sem.getEngineByName("LANGUAGE_NAME");  // 
language name used for JSP
    "language" attribute in the page directive
                    // run (execute) the script with the ScriptContext
                Object res=se.eval(scriptFromJSP, sc);  // run the script
            }
            catch (ScriptException sexc)   // an error occurred
            {
                sexc.printStackTrace();
                System.exit(-1);
            }

    ... cut ...

---rony

Reply via email to