Current location - Quotes Website - Personality signature - JS embedded Java program
JS embedded Java program
With the appearance of java, it is particularly easy to embed javaScript scripts in Java. Java introduces a new javax script package, which provides a common interface for scripting languages.

Case; The idea of embedding javaScript scripts in novel network Java

Gets the manager of the script interpreter.

Get the interpreter instance ScriptEngine of js from the manager.

Gets the binding instance that stores javascript variables.

Pass a java io Reader stream and binding to the eval () method of ScriptEngine, run the script eval () method stored in an external file, and return the script running result. If an error occurs during execution, a ScriptException exception exception will be thrown.

A java program that runs javascript scripts

Import? Java io *; Import? Javax script binding; Import? Javax script ScriptEngine import? Javax script ScriptEngineManager imported? Javax script ScriptException/* * author? By who? id=? */Public? Class? RunScript? {male? Static electricity Invalid? main(String[]? args)? Throwing? io exception { ScriptEngineManager? scriptManager? =? New? script engine manager(); //There are many kinds of script interpreters in the manager that gets the interpreters. ScriptEngine? js? =? script manager getEngineByExtension(; js); //Get the interpreter of js from the manager//Define the script file string we want to run? File name? =? Null// Get the binding instance that stores javascript variables through the interpreter and provide it to the script binding? Binding? =? js create bindings(); //The processing parameter parameter is a variable parameter defined by the script and may contain Dname/value. Any parameter we want to process cannot start with d as (int? Me? =? ; I & ltargs length; i++){String? arg? =? args[I]; If(arg startsWith( D )){// If the parameter starts with d, handle int? pos? =? arg index of(=); If (pos? ==? )? Usage (); String? name = arg substring(pos); String? Value =? Arg substring (pos+); //Note that all variables we define are strings. If necessary, we can use java lang Number? A java language Boolean arbitrary Java object or NULL converts scripts into other types of bindings put(name? Value); //Variables in the script are stored in the binding instance }else{if(filename! =null) Usage (); File name = parameter; }}//This is to ensure that we get a file of parameter if(filename==null){usage (); }//Add a binding file name to tell the script engine that a special reserved variable name will be executed, which enables it to provide better error information binding put (script engine file name? File name); //A stream reader that reads files? Are you online? =? New? FileReader (filename); Try {//to execute the script and get the result. Note that in is equivalent to the script in js, and bindings are variable objects needed for script execution. The result? =? Js eval (in binding); The system outputs println (result); }catch(ScriptException? Ex){// If an exception occurs during execution, an error message System Out Println (ex) will be displayed; }} Static? Invalid? Usage () {System err println (usage:? java? RunScript[ Dname=value]? Script js); System exit (); //abnormal exit program, normal exit program uses system exit (); }}?

The Bindings object created in this code is not static. All variables created by JavaScript scripts are stored here. The following is a more practical example of scripted Java. It stores its Bindings object in a ScriptContext object with a higher scope so that its variables can be read, but the new variables will not be stored in the Binhdings pair. In this example, a simple configuration tool (text file) is implemented to define name/value pairs. You can query them through the configuration classes defined here. Their values can be strings or Boolean values. If a value is enclosed in curly braces, it will be passed to the JavaScript interpreter to calculate the values wrapped in the SimpleBindings object, so that the JavaScript interpreter can also access the values of other variables defined in the same file.

Lishi Xinzhi/Article/program/Java/JSP/20 13 1 1/ 19250