Communication between Java, Java Applet with JavaScript
Abstract: This article focuses on web development by using a flexible way to access Java from JavaScript language, methods and variable access JavaScript applet from JavaScript, JavaScript, and the use of technology in the Java Applet small program to achieve these types of Web development language is complementary to develop a more perfect web application.
JavaScript is a development language for HTML environments that provides scripts that can respond to web page events, which fully access all aspects of the browser window, good at merging HTML, Java Applet applet, insert, server program, and other web components, Form a fully integrated web application. And Java is a powerful famous development language that supports the development of web applications. It is used to develop advanced web objects and provide executable content embedded in the web page. It has a stronger programming function, which can be used as a good supplement to JavaScript features. However, these features are limited in a limited browser window area.
Java and JavaScript can complement each other to develop a more perfect web application. This article summarizes the use of the author's programming, involving some methods for the issue of visiting between JavaScript and Java, Java Applet, for reference only.
First, access Java method from JavaScript
In the HTML script, you can use JavaScript to directly call the method in Java, just use "java.lang." Before calling the statement. The specific statement is shown in the following example:
Java.lang.system.out.println ("Hello!")
But this method is only applicable to Netscape Navigator, which is generally not advocated in a web application.
Second, the methods and variables in the Java Applet applet from JavaScript
The JavaScript code can access the variables and methods of Java by JavaScript, the JavaScript code can access the variables and methods of Java, the specific steps, and the use of the law, as below. It should be noted that the Applet object provided by JavaScript has a Name property, without a method and event processor.
step:
1) The method and variable of the Java Applet applet to be accessed is declared as public, and must be declared in the public class;
2) The Java Applet applet should be loaded first to access, although the Applet object does not define an OnLoad event, but you can specify the ONLOAD event processor using the Window object in the
flag of the HTML document;3) The Java Applet applet can be accessed with the JavaScript Applet object.
Example:
"Mytest.htm"
Function AccessApplet ()
{setTimeout ("ChangeText ('I Like JavaScript!')", 1000);
Settimeout ("ChangeText ('I Like JavaScript and Java!')", 2000);
SetTimeout ("ChangeText ('i like java!')", 3000);
Settimeout ("AccessApplet ()", 4000);
}
Function ChangeText (s)
{
Window.Document.testText.Settext (s) // calls the setxt () method of the JavaApplet applet
// Note that the name of the applet must be identified by the
}
script>
hEAD>
// Put the applet by calling AccessApplet ()
applet>
body>
html>
"TestText.java"
Import java.applet. *;
......
Public Class TestText Extends Applet
{...
Public void setText (String S) // setText () must be declared as "public"
{
Text = s;
Repaint ();
}
}
Third, use JavaScript in the Java Applet applet
Live Connect provides interfaces with Java and JavaScript, allowing you to use JavaScript in the Java Applet applet. Specific steps are as follows:
1) Use the "MAYScript property of the
2) Import the Netscape. JavaScript package into the Java Applet applet;
3) Creating the handle of the JavaScript window in the GetWindow () method of the JSOBJECT class in the Java Applet applet;
4) Access the JavaScript object in the JSObject class in the Java Applet applet;
5) Call the JavaScript method with the Eval () method of the JSObject class in the Java Applet applet.
Example:
"Readform. Java"
Import Netscape.javascript.jsobject;
Import netscape.javascript.jsexception; // can be allowed to process exception events in a small program
......
Win = jsobject.getWindow (this); // Get the JavaScript window handle, reference the current document window
DOC = (jsobject) Win.getMember ("Document"); // Access JavaScript object
Form = (jsobject) Doc.getMember ("textform");
TextField = (jsobject) Form.getMember ("TextField");
Text = (string) TextField.getMember ("Value"); // Get the value of the text area
......
Win.eval ("Alert (/" this alert comes from java! / ")")
// Call the Alert () method of JavaScript
......
"User.htm"
......