ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); |
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.put("age", 21); engine.eval( "if (age >= 18){ " + " print(’Old enough to vote!’); " + "} else {" + " print (’Back to school!’);" + "}"); > Old enough to vote! |
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.put("age", 21); engine.eval(new FileReader("c:/voting.js")); |
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.put("age", 26); engine.put("noClaims", Boolean.TRUE); Object result = engine.eval( "if (age < 25){ " + " riskFactor = 1.5;" + "} else if (noClaims) {" + " riskFactor = 0.75;" + "} else {" + " riskFactor = 1.0;" + "}"); assertEquals(result,0.75); } |
public void testEvalWithBindings() throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); Bindings bindings = engine.createBindings(); bindings.put("age", 26); bindings.put("noClaims", Boolean.TRUE); bindings.put("riskFactor", 1); engine.eval( "if (age < 25){ " + " riskFactor = 1.5;" + "} else if (noClaims) {" + " riskFactor = 0.75;" + "} else {" + " riskFactor = 1.0;" + "}"); double risk = bindings.get("riskFactor"); assertEquals(risk,0.75); } |
engine.eval("importPackage(java.util); " + "today = new Date(); " + "print(’Today is ’ + today);"); |
engine.put("name","John Doe"); engine.eval( "name2 = name.toUpperCase();" + "print(’Converted name = ’ + name2);"); > Converted name = JOHN DOE |
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); Compilable compilable = (Compilable) engine; CompiledScript script = compilable.compile( "if (age < 25){ " + " riskFactor = 1.5;" + "} else if (noClaims) {" + " riskFactor = 0.75;" + "} else {" + " riskFactor = 1.0;" + "}"); Bindings bindings = engine.createBindings(); bindings.put("age", 26); bindings.put("noClaims", Boolean.TRUE); bindings.put("riskFactor", 1); script.eval(); |
public double calculateRiskFactor(int age, boolean noClaims) { double riskFactor; if (age < 25) { riskFactor = 1.5; } else if (noClaims) { riskFactor = 0.75; } else { riskFactor = 1.0; } return riskFactor; } |
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.eval("function increment(i) {return i + 1;}"); Invocable invocable = (Invocable) engine; Object result = invocable.invoke("increment", new Object[] {10}); System.out.print("result = " + result); > result = 11 |
//instantiate a java object = new Java( java.util.Date ); //call a method =->toString(); //display return value echo(); |
<ul> <? //display session attributes in table =->getSession()->getAttributeNames(); foreach ( as ) { = ->getSession()->getAttribute(); print("<li> = <li>"); } ?> </ul> |