Use Rhino to write SSJS(Server side javascript)
2012-06-28 15:44
525 查看
package jason.Rhino.study; import java.io.File; import java.io.FileReader; import java.io.IOException; import org.mozilla.js.Context; import org.mozilla.js.ContextAction; import org.mozilla.js.ContextFactory; import org.mozilla.js.ImporterTopLevel; import org.mozilla.js.Scriptable; public class ScriptManager { private Scriptable scope = null; private String beanName = null; public ScriptManager(final String beanName, final Object beanObj) { this.beanName = beanName; ContextFactory.getGlobal().call(new ContextAction() { @Override public Object run(Context cx) { scope = new ImporterTopLevel(cx); Scriptable wrapped = Context.toObject(beanObj, scope); scope.put(beanName, scope, wrapped); return null; } }); } public Object exec(final String script) { ContextFactory.getGlobal().call(new ContextAction() { @Override public Object run(Context cx) { return cx.evaluateString(scope, script, null, 0, null); } }); return null; } public void exec(File scriptFile) throws IOException { FileReader in = new FileReader(scriptFile); StringBuffer strBuf = new StringBuffer(""); char[] buf = new char[2048]; int length = -1; while ((length = in.read(buf)) != -1) { strBuf.append(buf, 0, length); } String scriptStr = strBuf.toString(); in.close(); in = null; strBuf = null; buf = null; exec(scriptStr); } public void destroyBean() { scope.delete(this.beanName); } public static void main(String[] args) throws Exception { CheckDataTampered check = new CheckDataTampered(); ScriptManager js = new ScriptManager("check", check); File file = new File("./src/jason/Rhino/study/test.js"); if(file.exists()) { js.exec(file); }else { System.out.println("file not found"); } } }
package jason.Rhino.study; import java.util.ArrayList; import java.util.List; public class CheckDataTampered { private List<String> lsFields = new ArrayList<String>(); public void setProtectedField(String fieldName) { lsFields.add(fieldName); } public void printOut() { for(String field : lsFields) { System.out.println(field); } } }
test.js
check.setProtectedField("111111111"); check.setProtectedField("222222222"); check.setProtectedField("333333333"); check.setProtectedField("444444444"); check.setProtectedField("555555555"); check.setProtectedField("666666666"); check.printOut();
js.jar is needed.
相关文章推荐
- Learning Server-Side JavaScript with Node.js
- Node.js: What is the best "full stack web framework" (with scaffolding, MVC, ORM, etc.) based on Node.js / server-side JavaScript? - Quora
- Use .NET SocketAsyncEventArgs to write socket server code in C#
- Use the SQL Server CLR to Read and Write Text Files
- [Poi] Use Poi to Build an Index.js with Modern JavaScript Features
- Use Node.js DDP Client on Arduino Yun to Access Meteor Server
- use noscript html tag when user disable the javascript in browser, guide user how to enable the js in different browser and retu
- Why we made vorlon.js and how to use it to debug your JavaScript remotely
- Use Node.js DDP Client on Arduino Yun to Access Meteor Server
- Use Jquery to get json data from the server-side php page
- use nodeJs to run your server
- python use dom to write xml file
- How to Use ipmitool to remote access SuperMicro Server
- MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
- [Nuxt] Build a Vue.js Form then use Vuex Actions to Post to an API in Nuxt
- ORA-32001:write to SPFILE requested but no SPFILE is in use
- Server 2008 R2: “You must use the Role Management Tool to install or configure Microsoft .NET”
- [MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API
- 用JavaScript(JScript)写ASP程序(build asp with server-side javascript)的优缺点及经验总结。
- Want to write a book? Use word count to stay on track