CHAPTER 9 SCRIPTING AND JSR 223 175
CHAPTER 9 SCRIPTING AND JSR 223 175 “var date = new Date();” + “date.getHours();”); String msg; if (hour < 10) { msg = "Good morning"; } else if (hour < 16) { msg = "Good afternoon"; } else if (hour < 20) { msg = "Good evening"; } else { msg = "Good night"; } Console console = System.console(); console.printf("Hour %s: %s%n", hour, msg); } catch (ScriptException e) { System.err.println(e); } } } Depending upon the current time of day, you ll get different results. > java RunJavaScript Hour 8.0: Good morning The last thing to really demonstrate in the API here is Bindings. First off is the primary reason to use Bindings: they offer the means of passing Java objects into the scripting world. While you can certainly get the Bindings object for a ScriptEngine and work with it as a Map, the ScriptEngine interface has get() and put() methods that work directly with the bindings of the engine. The FlipBindings class in Listing 9-3 shows the indirect use of the Bindings class. The program accepts a single command-line argument, which is passed into the JavaScript engine via a binding. In turn, the JavaScript reverses the string and passes the results out as a different binding. The reversed string is then displayed to the user. Listing 9-3. Reversing a String Through ScriptEngine Bindings import javax.script.*; import java.io.*;
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services