Menu

[r1115]: / trunk / php-java-bridge / WebContent / jsr223.jsp  Maximize  Restore  History

Download this file

41 lines (34 with data), 1.5 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
40
<%@page import="javax.script.*"%>
<%@page import="php.java.script.servlet.PhpServletScriptContext"%>
<%!
private static CompiledScript scriptCached;
private static synchronized CompiledScript getScript(Servlet servlet, ServletContext application, HttpServletRequest request, HttpServletResponse response) throws ScriptException {
if (scriptCached!=null) return scriptCached;
// get a script engine
ScriptEngine engine = new ScriptEngineManager().getEngineByName("php");
// set the servlet environment. This is important, otherwise we would not attach to the servlet context.
engine.setContext(new PhpServletScriptContext(engine.getContext(),servlet,application,request,response));
// compile and return a handle to it
return ((Compilable)engine).compile("<?php echo 'Hello '.java_context()->get('hello').'!<br>\n'; ?>");
}
%>
<%
CompiledScript script = null;
try {
script = getScript(this, application, request, response);
// display hello world
script.getEngine().put("hello", "eval1: " + Thread.currentThread());
script.eval();
script.getEngine().put("hello", "eval2: " + Thread.currentThread());
script.eval();
script.getEngine().put("hello", "eval3: " + Thread.currentThread());
script.eval();
script.getEngine().put("hello", "eval4: " + Thread.currentThread());
script.eval();
out.println("thread ended: " + Thread.currentThread());
} catch (Exception ex) {
out.println("Could not evaluate script: "+ex);
} finally {
if (script!=null) ((java.io.Closeable)script.getEngine()).close();
}
%>