Menu

[r657]: / trunk / php-java-bridge / examples / php+jsp / jsp+php.jsp  Maximize  Restore  History

Download this file

40 lines (33 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%!
/* The following code makes sure that the PHP script is generated with the servlet instance.
When the servlet is generated, a file jsp+php.jsp._cache_.php appears */
private static java.io.Reader script;
private static java.io.Reader getScript(String path) {
if(script!=null) return script;
return script=php.java.script.EngineFactory.getPhpScript(path,
new java.io.StringReader(
"<?php function f($arg) {return 1 + java_values($arg); }; ?>"));
}
%>
<%
/* Create a standard script engine */
javax.script.ScriptEngine e =
php.java.script.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" */
e.eval (getScript(application.getRealPath(request.getServletPath())));
/* make the script engine invocable */
javax.script.Invocable i = (javax.script.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)});
/* release the script engine, flushing its output */
e.eval ((java.io.Reader)null); // 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);
%>