<%@page import="java.io.*" %>
<%@page import="javax.script.*" %>
<%@page import="php.java.script.servlet.EngineFactory" %>
<%!
private static File script;
private static File getScript(String path) {
if(script!=null) return script;
return script=EngineFactory.getPhpScript(path,
new StringReader(
"<?php function f($arg) {return 1 + java_values($arg); }; ?>"));
}
%>
<%
/* Create a standard script engine */
ScriptEngine e =
EngineFactory.getInvocablePhpScriptEngine (this,
application,
request,
response);
/* and connect its standard output */
e.getContext().setWriter (out);
/* evaluate the script, cache it in the file ".../jsp+php.jsp._cache_.php" */
File script = getScript(application.getRealPath(request.getServletPath()));
FileReader reader = EngineFactory.createPhpScriptFileReader(script);
e.eval (reader);
reader.close();
/* make the script engine invocable */
Invocable i = (Invocable) e;
/* and invoke its phpinfo and f() functions */
Object result = null;
result = i.invokeFunction("phpinfo", new Object[]{});
result = i.invokeFunction("f", new Object[]{new Integer(2)});
/* terminate the script engine and flush its output, get its result code */
((Closeable)e).close (); // flush the output generated by invokeXXX
/* print the result from the above f() method invocation */
out.println("result from php::f(), printed from the servlet: " + result);
%>