Implementing applets to communicate with each other across frames and browser windows

Source: Internet
Author: User
Tags html page

Summary

You might think that the only option for an applet to communicate with each other is to use Getapplet. Unfortunately, the Getapplet method only returns applets that are on the same HTML page as the applet that emits the call, which limits the way you build an interesting interface through the communication between applets. The workaround for this tip is to enable applets in different frames and even in different browser windows to invoke each other's methods.

The Appletcontext class in the Java.applet package contains two member methods, namely, Getapplet and Getapplets. By using these two methods, an applet can find other applets and invoke their methods. To do this, you must meet the following security requirements:

These applets come from the same directory on the same server.

These applets run on the same page in the same browser window.

This can be a good reason to design security restrictions, but the latter requirement limits the way the use of communication between applets is used to make interesting applets.

Try to consider a situation in which:

You've just compiled a good stock market trading applet and decided to write a good help system for it. You want the Help system to be an applet, and you want to run it in a different browser frame from the Stock Exchange applet. You may make this decision either for the sake of the structure of the site or for the need to always show the help system. In addition, you want to enable the Help system to go to the correct information/guidance (like "paper clips" in the Microsoft Office Suite) based on the user's current actions in the stock trading applet. You even plan to prepare wizards in the Help system, which can point out problems remotely and can perform tasks remotely in the stock market trading applet.

The ideas embodied in this plan are very good. However, because these two applets are on different pages, the Java APIs in Appletcontext do not help you realize this idea-but this technique can help you.

Using the Appletcontext API

Before explaining the alternative mechanism for communicating between applets, I will first briefly explain how the two methods of Getapplet and getapplets work. An applet can find another applet in the same HMTL page by name by using the Getapplet method, and you can find all applets on the same page by using the Getapplets method. These two methods, if executed successfully, return one or more Applet objects to the caller. Once the caller finds an applet object, it may invoke the public method of the applet.

Suppose you have the following HTML code:

<applet code="Applet1" width="400" height="100" name="app1">
  </applet>
  <br>
  <applet code="Applet2" width="400" height="100" name="app2">
  </applet>
  <br>

By using the name attribute in the applet tag, you can refer to a specific applet in the following way:

Applet Theotherapplet = Getapplet ("App1");

Theotherapplet.anymethod (); Invoke any common method

Alternatively, you can use the following code to retrieve all applets on this page:

  Enumeration allAppletsOnSamePage = getApplets();
  while(allAppletsOnSamePage.hasMoreElements()) {
    Applet appl = (Applet) allAppletsOnSamePage.nextElement();
    appl.anyMethod(); //调用任一个公用方法
  }

When the calling applet retrieves one or more applets on the same HTML page on which it resides, it can invoke the common methods of these applets.

Related Article

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.