12.3 use Java in Javascript
Using JavaScript, you can use Java Applet, plug-ins, and
Hierarchical links are organically linked with various web elements, including images.
Javascript uses an array of Applet objects called applets to access Java applets. Applets count
A group is an attribute of the browser object document. Every applet object in this array follows them
The order of appearance in the HTML source code, with the value starting from 0.
<APPLET code = "blink. Class" name = "Blink" width = 300 Height = 100>
Then, the applet object in the corresponding JavaScript language can be in a format similar to document. Applets [0 ].
You can also use document. Blink for access. Blink is the name of this small application.
In special cases, Java attributes cannot be accessed through the network in Javascript, that is
Javascript can only call various public variables and attributes in local small Java applications. This design
To ensure Java security.
In addition to accessing small Java applications in Javascript, you can also use JavaScript to access
Any class in the Java package namespace. The concepts of classes mentioned in Java are the same.
The object concepts in JavaScript are consistent.
Tip: various objects in Java are organized into classes, and classes are organized into packages. subclasses in the package are organized from their base classes.
Inherit attributes and methods, and expand and change them.
Any Java class can use a simple syntax to correspond to an object in JavaScript. Example
For example:
VaR policystem = java. Lang. system;
The javasystem object in Javascript corresponds to the java. Lang. System Object in Java
Directly access various methods and attributes in Java. Lang. System in JavaScript. For example
Use in Javascript
Policystem. Err. println ("calling Java from JavaScript !! ");
To output a string using the Java method.
In addition to using the classes provided by Java, you can also dynamically create a Java
Class instance, as if this class is defined by JavaScript itself. For example, in Javascript
Create a date class in Java:
VaR mynewdate = new java. util. Date ();
In this way, the created mynewdate object can access data of all Java date classes.
By using JavaScript to directly access the Java class library, you can greatly expand
The use of JavaScript makes it no longer limited to a narrow range, but can have the ability of Java
Most functions, such as network access and complex window responses.
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 ("timesroman", Font. Bold, 60 );
String text;
Public void Init (){
TEXT = new string ("this is a small Java application that can be controlled in JavaScript. ");
}
Public void settext (string newtext ){
TEXT = newtext;
Repaint ();
}
Public void paint (Graphics g ){
G. setfont (f );
G. drawstring (text, 15, 50 );
}
}
This small application provides a public method called settext (), which can set this small
The text displayed by the program is used in JavaScript to control
Input.
Example:
<HTML>
<Body>
<Form name = "javascriptform">
<Input type = "text" name = "text1">
<Input type = "button" value = "appears in a Java Applet"
Onclick = "document. java1.setnewtext (document. javascriptform. text1.value );
">
</Form>
<APPLET name = "java1" code = "controljava. Class" width = 450 Height = 125>
</APPLET>
</Body>
</Html>
Javascript accepts a string as long as it is entered on the webpage. Because Java Applet
Use the program to provide a public interface method setnewtext (), the newly entered string can also be
Small Java applications are accessed.
The Java language also allows you to directly call Javascript methods and access JavaScript objects from the applet.
.
Because of the security mechanism of Java, you must explicitly specify that the access to the applet is relatively insecure.
JavaScript code, which can be achieved by adding the mayscript attribute to the <APPLET> tag. Example
For example:
<APPLET code = "Some. Class" width = Java Applet "mayscript>
In Java, the class library for processing Javascript communication is Netscape. Javascript .*. Set this package
Jsobject class and jsexception object. All objects used in javascript can be expressed
Netscape. Javascript. jsobject instance.
After preparing this package in Java, you can access the attributes of a JavaScript Object in two steps:
Create a Netscape. Javascript. jsobject instance to correspond to objects in Javascript;
Use the getmember () method of the jsobject class to access the attributes in the corresponding JavaScript.
Tip: If the JavaScript property obtained through the getmember () method is an object
The operation can also proceed.
Various browsers and interface objects in JavaScript are hierarchical. At the top of the object dependent table, the rule is
The browser Object window, followed by the Document Object. This object can accommodate the boundaries in the form and form.
Surface object. Each lower-level object is an attribute of the upper-level object. Therefore, you can use
Use the following code to obtain an interface object in javascript:
Jsobject into whandle = jsobject. getwindow (this );
Jsobject using enthandle = (jsobject) using whandle. getmember ("document ");
Jsobject formhandle = (jsobject) using enthandle. getmember ("myform ");
Jsobject buttonhandle = (jsobject) formhandle. getmember ("mybutton ");
After obtaining this JavaScript Object, you can obtain or set the object attributes.
To call related methods of JavaScript objects, you can use one of the following two methods:
Use the call ("methodname", arguments) method in the jsobject class: This Java method accepts
The method name and necessary parameters of the JavaScript Object are automatically called
Method.
Use the eval ("expression") method in the jsobject class: This Java method accepts a string, which
A string is composed of JavaScript commands. This method automatically executes the commands in this string.
Like this command in JavaScript.
For example, the alert () method used by the Java program to access the window object in Javascript in code
Import Netscape. Javascript. jsobject;
Import Netscape. Javascript. jsexception;
Public class jsalert extends java. Applet. Applet implements 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 Prospects
The limitations mentioned here refer to the limitations of Javascript in terms of security processing, and the second to the limitations of JavaScript
Functional limitations.
JavaScript Security defects reported mainly include:
The Javascript script can read the historical URL Information on the user's computer.
Send the information to a remote server.
The Javascript script can obtain the file list on the hard disk of your computer.
The Javascript script program can be sent by the user's e-mail account without the user's knowledge.
E-mail.
Because javascript can only run in a small space of the browser, in addition to a particularly important place
Users do not have to worry too much about what the Javascript program on the web page will bring to users' computers.
At least javascript programs cannot forcibly format users' hard disks beyond browser restrictions.
Some of the small security risks mentioned above will also be gradually clarified in future JavaScript versions.
Division.
The second limitation of JavaScript is its function:
Javascript is only a scripting language, and has very weak functions in graphics and multimedia processing.
If you want to implement relatively complete media operations, a better solution is to use a small Java application.
Program.
Javascript is interpreted and executed, so the execution speed is very slow, which is a common problem in all scripting languages.