Calling Java from JavaScript and vice-versa why? -


I know that we can call Java from Java and on the contrary, but I am thinking that the power When to take it and use it in a healthy way I am talking about real world applications, not "hello world".

Many developers are usually developing Java and Javascript but I do not have enough experience to mix those people, so this is a good idea.

Please be a developer who can share your knowledge about the real world applications that have been created using this mixture :)

Forgiveness, I failed to express my doubts, for example, have you heard? For example, you can call javascript code from Java class, but I do not see a business case, in which those capabilities increase the value of my server side applications.

This is a drunk hello world, this application has a Java menu that has the contents of the .js file called "eval" inside the js file.

nashorn hello world app

My main doubt is that The power will really be useful

web application development (which is the usual scenario where Java and JavaScript chat):

There are two places where you can create servers and customers. Side languages ​​interact with each other:

  1. At the time of rendering, this is when the user Generate HTML output.

  2. At runtime, this is when HTML is generated and it is on the client side (such as browser client). For the first case, suppose we have a service that adds an attribute for the request and then moves a new (JSP) view forward. This is an example of servlet:

    @WebServlet ("/ hello") increases the public class HelloServlet HttpServlet {@Override throws Secure zero doPost (HttpServletRequest request, HttpServletResponse response) ServletException, IOException {String name = request.getParameter ("name"); // Create Message Request.setAttribute ("name", name); Request (request, response); }}

    Then in your view (Hello.zap file in the web-INF folder), you have such code:

      & lt ;! DOCTYPE html & gt; & Lt; Html lang = "en" & gt; & Lt; Top & gt; & Lt; Title & gt; Servlet Hello World & lt; / Title & gt; & Lt; Script & gt; Function changeName (var new name) {document.getElementById ('divName'). InnerHtml = 'hello' + new name; } Window.onload = function () {changeName ('$ {name}'); } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div id = "divName" & gt; & Lt; / Div & gt; & Lt; Form id = "name" action = "hello" & gt; Rename: & lt; Input type = "text" name = "name" /> & Lt; Br / & gt; & Lt; Input type = "text" value = "rename" /> & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

    By using this approach, we can make Java (variable) in Java directly while displaying the dialog. Therefore, we can access Java Pages, Requests, Sessions, and Reference Features only , when creating HTML going to the client.

    Note that if you want to turn it back on Java code, you should make a new request on the server to execute the Java code. This is where Ajax is accessible.

    Ajax lets you communicate with asynchronous server side, the server will respond to your request, and then determine how you will use it on the client side. For this purpose, it is better to use a common format for communication. The most preferred format nowadays is the Ajax Interaction of servlet is widely covered here: (There is no need to change the wheel again in this post).


    Standalone or mobile application :

    Java clients will run on the machine Note that Java can execute Java code by some means Or is it useful when you already have a lot of functionality written in javascript (external library) and do not want to migrate all code to java by executing the code in java in your java application The area just ScriptEngineManager and StringEngine can use sections. A real-world example of using Javascript in the Java application is written in Java, which allows to implement the conversion in your code via JavaScript scripts.

    More information about NASA:


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -