Consolidating Java functions in JavaScript programs

Source: Internet
Author: User
Tags html page

The question is presented as follows:

Suppose that there are some forms in our HTML page that need to be processed, and we need to initialize the fields in the database, what should we do? The standard solution is to use CGI scripts or server-side programs such as the Java servlet, but have you ever thought of writing a script that allows you to use JavaScript directly to invoke a server-side Java program for any computational results, as listed in the following code:

我们的表单<br>
<form>
<input type="text" name="textField"><br>
<input type="button" value="Click" onClick="updateField();">
</form>
<script>
function updateField()
{
document.forms[0].textField.value=java.dbConnection.getData('SQL_expr');
}
</script>

So, when the user clicks on the button, the program invokes the Java DbConnection class, and then it can use the Java returned results in the JavaScript application. The above program just updates the TextField value and we don't have to refresh the entire page. Since this program is not a JSP file, you do not have to compile your page into a Java Servlet.

You can also use the results returned by calling some Java functions to replace the text in a paragraph; in a very large HTML table, you can use the same method, as long as you update one line of information:

<script language="JavaScript">
function changeCol()
{
document.all.quote.rows[0].cells[1].innerText=java.Stock.getQuote('Wayne');
}
</script>
<table id="quote" border=1>
<tr><td>Wayne</td><td>123</td></tr>
<tr><td>Another one</td><td>34</td></tr>
</table>

If you can embed Java object calls directly into JavaScript and make sure that the rest of your page is not changed, you must be more interested in the problem. OK, now let's implement it with the Java servlet.

Working principle

My idea is to use the Java servlet to write a Jinj (Java in JavaScript) program that allows you to consolidate Java classes and JavaScript scripts within HTML pages, All HTML pages that use JavaScript to invoke Java functions are routed to this servlet, and Jinj will instantly produce hidden frames with Java applets, which act as bridges to Java communications, which are less than 2KB in size. To invoke an object, the applet uses HTTP to connect to the same Jinj Servlet. Jinj replaces all Java calls with the corresponding JavaScript structure (in fact, each invocation preceded by a prefix Java) and keeps the rest of the page unchanged. So, in order for Jinj to properly identify any Java calls in your JavaScript code, use the following structure: Java.object_name.function_name (list_of_parameters):

Java: is a standard prefix:

object_name: is a variable name that holds an instance of some Java class (in the end, we'll discuss how to create/define such a variable in detail), such as it can be a class name.

Function_name: is a function name for a Java class.

List_of_parameters: Is the parameter list of the called function (can be empty).

The following will also explore how to pass parameters to Java, each of which is clearly a JavaScript expression. You can also use a precompiled page, in other words, to compile it before using an HTML page.

Related Article

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.