From: <php...@li...> - 2010-09-25 10:10:40
|
Hi, [please excuse the delay] > instance.getEngine().getContext().setWriter(writer); // wrong! reset by following line > instance.getEngine().setContext((ScriptContext)new PhpHttpScriptContext(instance.getEngine().getContext(),this,this.getServletContext(),request,response)); > I have been doing more investigations into why the php-java bridge is > not working as I would expect The PHP/Java Bridge code is correct. The decorator you've set connects the writer, reader, errorWriter to the servlet. So your code has no effect. Please see the example code I've given. BTW: For PHP/Java Bridge 6.2.1 I will rename the decorator to "PhpHttpScriptContext". Please either use your own decorator (copy PhpHttpCompiledScriptContext.java to your project) or please use the new name. The test script I've used is below: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <%@ page import="java.io.*" %> <%@ page import="org.apache.commons.lang.*" %> <%@page import="javax.script.*" %> <%@page import="php.java.script.servlet.*" %> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Example</title> <% // My PHP script is defined here.... String phpScript = ""; phpScript += "function pass_to_php_backend($ar) { \n"; phpScript += " print('something'); \n"; phpScript += " //do something useful here...\n"; phpScript += "} \n"; phpScript += "function outputJavaScript() { \n"; phpScript += "$IMConfig = array(); \n"; phpScript += "$IMConfig['allowed_image_extensions'] = array('jpg','gif','png'); \n"; phpScript += "return pass_to_php_backend($IMConfig); \n"; phpScript += "} \n"; phpScript += "outputJavaScript(); \n"; %> <%! private static final CompiledScript script; static { try { script =((Compilable)(new ScriptEngineManager().getEngineByName("php"))).compile( "<?php echo eval(java_context()->get('script')); ?>"); } catch (ScriptException e) { throw new RuntimeException("bleh!"); } } // private static final String evalPHP(String arg) { 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().setContext((ScriptContext)new PhpHttpScriptContext(instance.getEngine().getContext(),this,this.getServletContext(),request,response)); instance.getEngine().getContext().setWriter(writer); 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; } %> <script type="text/javascript"> a = '<%= evalPHP(phpScript, request, response) %>;' if (a == "") alert("No Output"); else alert("Output = " + a); <%= evalPHP(phpScript, request, response) %> </script> </head> <body> hello </body> </html> Regards, Jost Bökemeier |