From: <php...@li...> - 2010-09-14 20:03:57
|
On 9/12/2010 3:48 AM, php...@li... wrote: > Hi again, > > insert a > > instance.getEngine().setContext(new > PhpCompiledHttpScriptContext(instance.getEngine().getContext(),this,application,request,response)); > > before calling instance.eval(). Otherwise JSR223 will use the default > context, which isn't very usable in a web-context. Hi Jost - Hmmmm since the method evalPHP, as you sent it to me in your previous email, is declared private static final, one cannot use the 'this' parameter. Do you see any problem with removing the static final from the declaration? That allows this to be called in the context of the instance of the servlet that is running. But doing so will then cause the parameters of evalPHP - application, request, and response to be undefined since this code is within the Declaration section of the servlet. I think the application parameter can be retrieved via a call to this.getServletContext(), but the only way to get the request and response parameters will be to pass them in to the evalPHP method. Do you concur? I am thinking the following - private String evalPHP(String arg, HttpServletRequest request, HttpServletResponse response) { CompiledScript instance = (CompiledScript)((java.security.cert.CertStoreParameters)script).clone(); instance.getEngine().put("script", arg); OutputStream out = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(out); instance.getEngine().getContext().setWriter(writer); instance.getEngine().setContext((ScriptContext)new PhpCompiledHttpScriptContext(instance.getEngine().getContext(),this,this.getServletContext(),request,response)); try { instance.eval(); } catch (ScriptException e) { throw new RuntimeException("bleh!"); } try { writer.close(); } catch (IOException e) { throw new RuntimeException("bleh!"); } String result = out.toString(); return result; } I have gone ahead and tried this, and it almost got me to what I want. But one problem remains and I don't grok the code enough to be able to solve on my own, so need a bit more guidance. The PHP script that I want to embed in the middle of a Javascript section generates a line of Javascript that I want the servlet to write back out, at the point where the call to evalPHP is made. But when I execute this jsp servlet, the generated Javascript from the PHP script is being put out at the very beginning of the document. (I was also surprised that the return result from evalPHP is null....) Thanks again for all your help! Marc... |