Communication between Java, Java Applet and JavaScript

Source: Internet
Author: User
Abstract: This article focuses on the methods for accessing Java from the Javascript language and the methods and variables for accessing JavaScript applets from Javascript in Web development, and the use of JavaScript and other technologies in Java applet to complement these web development languages to develop more perfect web applications.
Javascript is a development language used in the HTML environment. It provides scripts that can respond to web page events and completely accesses all aspects of browser windows, good at merging HTML, Java Applet applets, inserts, server programs and other Web components to form fully integrated web applications. Java is a well-known powerful development language that supports the development of Web applications. It is used to develop advanced web objects and provides executable content embedded into web pages. It has stronger programming functions, it can be a good supplement to JavaScript Functions, but these functions are limited in a limited browser window area.
Java and javascript can complement each other to develop more perfect web applications. This article summarizes some of the methods that I have used in programming that involve mutual access between JavaScript and Java Applet applets for reference only.
1. Access Java methods from Javascript
In an HTML Script, you can use JavaScript to directly call methods in Java. You only need to use "Java. Lang." Before calling a statement. The statement usage is as follows:
Java. Lang. system. Out. println ("Hello !")
However, this method is only applicable to Netscape Navigator and is generally not recommended in Web applications.
2. Access methods and variables in Java Applet from Javascript
Through the applet object provided by JavaScript, JavaScript code can access Java variables and methods. The specific steps and usage examples are as follows. It should be noted that the applet object provided by JavaScript has the name attribute, and there is no method or event processor.
Steps:
1) the methods and variables of the Java applet to be accessed are declared as public and must be declared in the public class;
2) the Java Applet can be accessed only after it is loaded. Although the applet object does not define the onload event, it can be found in the "body" flag of the HTML document, specifies the onload event processor of the window object;
3) Java Applet can be accessed using JavaScript applet objects.
Example:
Using mytest.htm"
<HTML>
<Head>
<Script language = "JavaScript">
Function accessapplet ()
{SetTimeout ("changetext ('I like JavaScript! ') ", 1000 );
SetTimeout ("changetext ('I like JavaScript and Java! ') ", 2000 );
SetTimeout ("changetext ('I like Java! ') ", 3000 );
SetTimeout ("accessapple()", 4000 );
}
Function changetext (s)
{
Too many Doc ument. testtext. settext (s)
// Call the settext () method of the Java applet.
// Note that the applet name must be identified by the "applet" flag name attribute
}
</SCRIPT>
</Head>
<Body onload = "accessapplet ()">
// Call accessapplet () to load the Applet
<APPLET code = "testtext. Class" name = "testtext" width = 450 Height = 150>
</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 ();
}
}
3. Use Javascript in Java Applet
Live connect provides interfaces between Java and JavaScript, allowing JavaScript to be used in Java applet. The procedure is as follows:
1) Use the mayscript attribute of the <APPLET> flag in HTML scripts to allow Java Applet applets to access the script;
2) import the Netscape. Javascript package into the Java applet;
3) use the getwindow () method of the jsobject class in the Java applet to create the handle of the Javascript window;
4) use the getmember () method of the jsobject class in the Java applet to access JavaScript objects;
5) use the eval () method of the jsobject class in the Java applet to call the JavaScript method.
Example:
"Readform. Java"
Import Netscape. Javascript. jsobject;
Import Netscape. Javascript. jsexception; // allows you to handle abnormal events in a Applet
......
Win = jsobject. getwindow (this); // obtain the Javascript window handle and reference the current document window
Doc = (jsobject) win. getmember ("document"); // access JavaScript objects
Form = (jsobject) Doc. getmember ("textform ");
Textfield = (jsobject) form. getmember ("textfield ");
TEXT = (string) textfield. getmember ("value"); // obtain the value of the partition Area
......
Win. eval ("alert (\" this alert comes from Java! \")");
// Call the alert () method of JavaScript
......
Login user.htm"
......
<Body>
<Form name = "textform">
<P> Enter text and then click display text:
<Input type = "text" name = "textfield" size = "20"> </P>
</Form>
<APPLET code = "readform. Class" width = 200 Height = 100 name = "readapp" mayscript>
// The mayscript attribute flag must be used.
</APPLET>
</Body>
The appendix lists the methods and descriptions of the jsobject class. For more details, refer to msdn. It is worth noting that the program example in this article is underlined.

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.