Use Java in JavaScript

xiaoxiao2021-03-06  41

12.3 Using Java in JavaScript

With JavaScript, you can use a small application (Applet), plugin, and a plurality of forms, hyperlinks, and images including forms, hyperlories, and images including forms, hyperlories.

JavaScript uses an array called Applets to access Java's Applet. The Applets array is an attribute of the browser object Document. Each of this array is arranged in the order in which they appear in the HTML source code, and the debate value starts at 0.

The Applet object in the corresponding JavaScript language can be represented in the form of document.applets [0], or you can use Document.blink to access, Blink is the name of this applet.

There is a special situation that cannot access Java properties through the network in JavaScript, that is, JavaScript can only call various common variables and properties in the local Java applet. Such a design is to protect JAVA's security.

In addition to accessing Java's small apps in JavaScript, you can use JavaScript to access any class of the Java package name space. The concept of class (Class) mentioned in the Java language is consistent with the object concept in JavaScript.

Tip: Various objects in Java are organized to categorically, and the classes are organized into packages, and the subclasses in the package are inherited from their base classes, and expand and change.

Classes in any Java can use a simple syntax corresponding to an object in JavaScript. For example: Var javasystem = java.lang.system;

The object JavaSystem in JavaScript corresponds to the java.lang.system object in the Java language, which is directly accessed in JavaScript to access various methods and properties in java.lang.system. For example, JavaSystem.err.Println can be used in JavaScript ("Java !!" from JavaScript); use Java's method to output a string.

In addition to the classes provided directly from Java, you can also create an instance of a Java class in JavaScript, as if this class is the same as the JavaScript itself. For example, you can create a Date class in Java in JavaScript: var mynewdate = new java.util.date (); this created MynewDate object can access all Java class Date data.

By using JavaScript's functionality directly accessing the Java class library, you can greatly extend the use range of JavaScript, so that it is no longer limited to small scope, and most of the functions that can be implemented in Java languages, such as network access And complex window response, etc.

Example: A small application import java.applet.applet; import java.awt.graphics; import java.awt.font;

Public class controljava extends applet {font f = new font ("Timeesroman", Font.Bold, 60); string text;

Public void init () {text = new string ("This is a Java's applet, which can be controlled in JavaScript.");

Public void settext {text = newText; repaint ();} public void Paint (graphics g) {g.setfont (f); g.drawstring (text, 15, 50);}}

This applet provides a public approach called setText (), which can set the text displayed by this applet, where this method is used in JavaScript to control the input of the Java applet.

Example:

JavaScript is responsible for accepting this string as long as you enter a string in the web page. Since the Java applet provides a public interface method setnewtext (), this new input string can be accessed by the Java applet.

The Java language also allows the approach to JavaScript methods and access to the JavaScript object to access JavaScript objects from the Applet.

Due to the security mechanism of the Java language itself, it is necessary to explicitly specify the Applet to access the relatively unsafe JavaScript code, which can be implemented by adding the MayScript property in the tag. For example:

Processing and JavaScript communication in Java language is Netscape.javaScript. *. This package defines the JSObject class and the JSException object. Objects used in JavaScript can represent instances of Netscape.javaScript.jsObject.

After preparing this package in Java, the properties of the JavaScript object can be done in two steps: Create a netscape.javascript.jsObject instance, used to correspond to objects in JavaScript; use the JSObject class's getmember () method to access the corresponding JavaScript Attributes.

Tip: If the JavaScript attribute obtained by such a getmember () method is an object, such an operation can be done.

Various browsers and interface objects in JavaScript are layered. The topmost of the object from the object is the browser object Window, and then the following is the Document object, this object can be contained in the form and the interface object in the form. Each lower object is an attribute of the upper object. Therefore, according to the method earlier, the following code may be used to obtain a JavaScript language interface objects: JSObject windowHandle = JSObject.getWindow (this); JSObject documentHandle = (JSObject) windowHandle.getMember ( "document"); JSObject formHandle = ( JSOBJECT) DocumentHandle.getMember ("MyForm"); JSObject ButtonHandle = (JSObject) FormHandle.getMember ("MyButton"); after getting this JavaScript object, you can get or set the properties of the object. In order to call the relevant method of the JavaScript object, one of the following methods can be used: Using the Call ("MethodName" method in the JSObject class: this Java method accepts the method name of the JavaScript object and the necessary parameters, automatic call Methods of corresponding to JavaScript objects. Using the Eval ("Expression") method in the JSObject class: This Java method accepts a string, this string consists of JavaScript command, this method automatically executes commands in this string, as if this command is in JavaScript The same.

Example: Java program accesses the Alert Netscape.javascript.jscape.javascript.jsexception; import netscape.javascript.jsexception; import netscape.javaScript.jsexception;

Public class jsalert extends java.applet.applet imports runnable {jsobject windowobject;

Public void init () {windowobject = jsobject.getwindow (this);

Public void main () {WindowObject.eval ("Alert (/" Hello, World! / ");");

Public void stop () {}

Public void paint () {}

Public void run () {}}

12.4 Limitations of JavaScript and future outlook

The limitations herein first refer to the limitations of JavaScript on security processing, followed by the functional limitations of JavaScript.

The reported JavaScript security defects mainly include: JavaScript scripter can read historical information on the URL on the user's computer, and send this information to the remote server without knowing. The JavaScript script can get a list of files on the user's computer hard drive. The JavaScript scripter can send E-mail using the user's E-mail account without knowing the user.

Since JavaScript can only run in a spatial space of the browser, in addition to particularly important places, users don't have to be too worry about browsing the JavaScript programs in the browsing web page will bring the user's computers, at least JavaScript programs cannot be browsed The limitations of the device forcibly format the user's hard drive. Some small safety hazards mentioned above will also be gradually cleared in future JavaScript release versions.

转载请注明原文地址:https://www.9cbs.com/read-63280.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.054, SQL: 9