Make communication between the applets of different pages

zhaozj2021-02-17  64

Java Tips 101: Alternative Method of Communication between Applet

Learn how to make the applet cross-frame and browser windows to communicate with each other

Author Tobias Hill

Summary You may think that the only choice allows Applets to communicate with each other is to use the getApplet. Unfortunately, the GetApplet method returns only the applet that is called the Applet on the same HTML page, which limits the communication to build an interesting interface between communications between the applet. The alternative method of this tip will make the approach to each other in different frameworks or even different browser windows.

Java.applet

The AppletContext class contains two member methods, namely

GetApplet and

GetApplets. By using these two methods, an applet can find other applets and call them. To do this, you must meet the following security requirements:

These applets are from the same directory on the same server. These applets are running on the same page in the same browser window.

This should be designed to have a good reason; however, the latter requires to limit the use of communication between the APPLET to make interesting multi-applet interface.

Try to consider such a situation:

You just have a good stock market trading applet and decide to write a good help system for it. You want help the system is also an applet and want it to run it with the stock market trading applet in different browser frames. You make this decision may be for website structure considerations, or it may be due to the need to help the system. In addition, you want to help the help system transfer to the correct information / guide based on the operations currently in the stock trading applet (just like the "paper-free needle" in the Microsoft Office Suite). You even plan to prepare your wizard in helping the system, which can remotely indicate the issue, and can remotely perform the tasks in the stock market trading applet.

The idea of ​​this program is very good. However, because these two applets are on different pages, the Java API in AppletContext cannot help you implement this idea - but this skill can help you.

Using the AppletContext API in front of the alternate mechanism of communication between the AppleT, I will first briefly explain how the getapplet and getapplets work. An applet can find the same page in the same hmrtl page by using the getapplet method, and find all the applets on the same page by using the getapplets method. If two methods are successfully executed, one or more applet objects are returned to the caller. Once a caller finds an Applet object, it may call this applet's utility.

Assume that there is such an HTML code below:


By using the name attribute in the Applet tag, you can reference a specific applet with the following manner:

Applet theoTheRapplet = getApplet ("app1"); the PortHERAPPLET.AnyMethod (); // Call either public method or you can also retrieve all Applets on this page with the following code:

Enumeration allAppletsOnSamePage = getApplets (); while (allAppletsOnSamePage.hasMoreElements ()) {Applet appl = (Applet) allAppletsOnSamePage.nextElement (); appl.anyMethod (); // call to a public method according to any}

When the calling applet retrieves one or more Applets on the same HTML page it is, it can call these applets.

Using a static data structure is unfortunately, if you use a standard method, you can only communicate with Applets in the same HTML page. Fortunately, you can easily avoid this limit. Make the approximate method between the AppleT across page communication based on the same fact, that is, if the two applets of CodeBase, even if they are loaded in different browser windows, they also share the same runtime environment. Roughly said that CodeBase is the directory that loads the applet. Please refer to the reference resource after the text, one link points to a tutorial about CodeBase.

Since the runtime environment is shared, all Applet instances can access static domains and static structures so that these static domains and structures can be used to pass information between different applets.

The Applet not only stores a simple data type such as an integer, characters, and strings, but also each applet can store one of its own (instance) in a static domain (possibly in its own class). Any applet can access this domain to get a reference to this instance.

Is this complicated? No, it is not complicated at all. I first raised a simple example. Assuming your applet (appleta.class) in a frame, and the other applet (appletb.class) is loaded from the same codebase in another frame.

You now want to grant AppletA permissions to access the utility of Appletb. You must let Applets store their own references in a static public domain, just like this:

Public class appletb {public static appletb selfref = null; // Initial zero PUBLIC VOID INIT () {// Generate references for this instance Selfref = this;} ...}

Now you can access Appletb from Appleta:

Public class appleta {appletb theotherapplet = null; public void callappletb () {// Get static domain, where the pointer to the // instance of Appletb is stored. The PortHERAPPLET = AppletB.selfref; // You can call an instance method, // ... the PortHerapplet.Repaint ();} ...} This is all our work. Because the runtime environment is shared by different applets, this method is equally active even if the applet is not on the same page.

It is worth noting that the above code is not processed to call the CalPpleTB method in Appleta before starting the appletb. If this happens, Selfref will be NULL, which cannot be made any communication.

A more common method of course, there is a more common way. You can create such a class that created its unique purpose is to store a reference to the Applet in a static data structure. You will see the AppleTlist class later. I hope that other applets access your own utility approach to register themselves through appletList. Follow the AppletContext.GetApplet (String Name), each registration item is associated with a string. When an applet calls a reference to an Applet, this string will play a keyword.

Typically, Applet is registered as follows:

Public class appleta {public void start () {AppleTList.Register ("stock-trade-applet", this); ...}}

Another applet gets access to it:

Public class appletb {public void run () {appleta tradeApplet = (appleta) AppleTlist.getApplet ("stock-trade-applet"); ...}}

When the applet stops running, you must keep your registration in AppleTlist:

Public void stop () {AppleTlist.remove ("stock-trade-applet"); ...}

The complete source code for the AppleTlist class is as follows:

0: Import java.util. *; 1: import java.applet.applet; 2: 3: public class appletlist {4: private static hashtable applets = new hashtable (); 5: 6: public static void register (String Name, Applet applet) {7: applets.put (name, applet); 8:} 9:10: public static void remove (String name) {11: applets.remove (name); 12:} 13:14: public static applet GetApplet (String Name) {15: return (applet) Applets.get (Name); 16:} 17:18: public static enumeration getapplets () {19: return applets.ements (); 20:} 21:22: public Static int size () {23: return applets.size (); 24:} 25:} To get an example of how to use this class, please download ExampleCode.zip in the reference resource.

The limitations are just as mentioned earlier, these Applets must be loaded from the same codebase. In addition, if the two different copies of the browser are running, and the applet is loaded into each copy, the applet may not be in communication with each other (depending on the version and settings of the browser), because they may no longer share the same runtime surroundings. However, there is no problem if the browser itself is derived from a new browser window.

This tip has been tested in several platforms and several browser versions, but the runtime environment in some configurations may be independent. This tip has passed the test in the following operating system and browser combination:

Windows 2000: Internet Explorer 5.0, Internet Explorer 5.5, Netscape Navigator 4.72, Opera 4.01Windows 98: Internet Explorer 4.72, Internet Explorer 5.0, Netscape Navigator 4.02Mac OS 9: Internet Explorer 4.5, Netscape Navigator 4.5Red Hat 6.2: Netscape Navigator 4.73

Summary This trick illustrates an alternative method that enables Applet to communicate with each other. This method works in a manner that the Java API's getapplet () method is not supported. The knowledge introduced in this technique increases the possibility of part of applet as part of the website or internal network - can use it to replace or supplement the getApplets method.

About the author Tobias Hill is one of the founders of Citerus. The company is based on Sweden, which is committed to constructing Internet, internal network and external network systems on the Java platform. Hill started with Java in 1996, participated in many projects, programmed to develop online flamework postcard manufacturing procedures, etc.

Reference resource

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

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