IT Acumens Forum for Discussion

Tech News => Interview FAQ's in Java => Topic started by: sukishan on Jul 15, 2009, 08:43 PM

Title: Sending Messages to Other Applets
Post by: sukishan on Jul 15, 2009, 08:43 PM
Sending Messages to Other Applets
Applets can find other applets and send messages to them, with the following security restrictions:

Many browsers require that the applets originate from the same server.
Many browsers further require that the applets originate from the same directory on the server (the same code base).
The Java API requires that the applets be running on the same page, in the same browser window.

An applet can find another applet either by looking it up by name (using the AppletContext getApplet method) or by finding all the applets on the page (using the AppletContext getApplets method). Both methods, if successful, give the caller one or more Applet objects. Once the caller finds an Applet object, the caller can invoke methods on the object.

Finding an Applet by Name: The getApplet Method
The getApplet method looks through all of the applets on the current page to see if one of them has the specified name. If so, getApplet returns the applet's Applet object.

By default, an applet has no name. For an applet to have a name, one must be specified in the HTML code that adds the applet to a page. You can specify an applet's name in two ways:

By specifying a NAME attribute within the applet's <APPLET> tag. For example:
<APPLET CODEBASE=example/ CODE=Sender.class
. . .

By specifying a NAME parameter with a <PARAM> tag. For example:
<APPLET CODEBASE=example/ CODE=Receiver.class
<PARAM NAME="name" value="old pal">
. . .