When JS runs on Java, there are those amazing performances!

Source: Internet
Author: User

Java development for more than 3 years, the understanding of Java is not very deep! During the period of contact with other languages, including Php,nodejs,expressjs and other languages, let me feel more deep is the definition of variables, in Java, strings to use string A, numbers to use int A; or double A; and PHP $ A can define all types of variables, Nodejs and Expressjs use Var A to define all types of variables, here you might say Java is more rigorous, and Java can also use object A to define all the variables Ah! In Java I believe that for the definition of a number int a=4, and no one is written as Object A = 4.

    • What if JS runs on Java?
      1. Variable definition can be var A to unify all the object and variable definition rules
      2. Object declaration becomes var b = {name: "Zhangsan", age:14};
      3. When the JS code changes, the code can be interpreted and executed immediately without recompiling the Java class
      4. You can use the function object var c = function () {return D;}
      5.js can invoke Java class methods or properties
      6. How to use a NoSQL database to manipulate objects directly!
    • here's how to make JS run on Java
      1. Let Java can explain Js-–>rhino this thing can explain JS
      The 2.JAVA base call code is as follows
publicstaticvoidmainnew ScriptEngineManager().getEngineByName("javascript");        Bindings bindings= js.getBindings(ScriptContext.ENGINE_SCOPE);bindings.put("java_out", System.out);js.eval("var a=‘hello‘;java_out.println(a);");}
    • let JS to implement the basic framework of Java Web Engineering implementation
      The directory structure is as follows

      Jsaction will forward the request to the Index.js file under Web-inf when the user accesses Http://localhost:8080/xxx/js/index
      The Jsaction code is as follows
 Public  class jsaction extends httpservlet {     Public void Doget(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {doPost (request, response); } Public void DoPost(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {scriptengine js =NewScriptenginemanager (). Getenginebyname ("JavaScript");        Bindings bindings= js.getbindings (scriptcontext.engine_scope); Bindings.put ("Java_out", System.out); Bindings.put ("Java_request", request); Bindings.put ("Java_response", response); Basedao DAO =NewBasedao (); Bindings.put ("Java_dao", DAO); Extension ext =NewExtension (REQUEST,RESPONSE,JS); Bindings.put ("Java_ext", ext);    Loadjs (request, JS); }Private void Loadjs(HttpServletRequest Request,scriptengine JS) {Try{String path = Request.getsession (). Getservletcontext (). Getrealpath ("/");//Get system pathString f = Request.getrequesturi (). Replace (Request.getcontextpath (),"")+". js"; System.err.println ("Request"+F); Js.eval (NewBufferedReader (NewInputStreamReader (NewFileInputStream (NewFile (path+"web-inf/"+F)),"Utf-8"))); }Catch(Exception e)        {System.err.println (E.getmessage ()); }    }}

The Index.js code is as follows

Java_ext.include ("Js/config.js");//jsonvarObj ={name:"Zhangsan", Age:"very", Sex:"Male"};java_out.println(Obj.age);//put or getJava_ext.put ("Sex", obj.sex); java_out.println(Java_ext.get ("Sex"));//db op//demo1varList=java_dao.query ("SELECT * FROM VIP limit 0,10",[]);var Map= List.get(0); Java_out.println(Map. Get ("Address"));//demo2varListCount = Java_dao.query ("SELECT COUNT (*) as C from VIP",[]);varCount = Listcount.get(0). Get ("C"); Java_out.println(count);//demo3varId=java_dao.execute ("INSERT into test2 values (?)",["Nihao"]); Java_out.println(ID);//demo4var BOOL= Java_dao.transaction ([INSERT into test2 values (' Zhangsan '),INSERT into test2 values (' Lisi ')]) java_out.println(BOOL);//go to TPLJava_ext.load (".. /tpl/index.jsp ");
    • isn't that funny? You have to try it!
      Original: Http://blog.csdn.net/qilin001cs

When JS runs on Java, there are those amazing performances!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.