Monday 11 May 2015

Inter-Applet Communication

 In the previous example, the button component was used to add items to the list component. All of these operations occurred inside a single applet. However, imagine a case where a button-click in one applet could update a database and trigger update messages to another applet running within the same Web page or even running on a remote computer. This type of communication is known as inter-applet communication. Since the early days of Java, this feature has been requested by developers wanting to push the envelope of Java's capabilities. This section briefly discusses several methods for communicating between applets, but you should note that these methods are browser-dependent and are subject to change.

Java Beans

Recently, a technology known as Java Beans has been introduced by JavaSoft. Java Beans is a component object model that will allow Java applets to be integrated with other applets and applications using multiple component architectures, including COM, OpenDoc, LiveConnect, and CORBA. This component model is supposed to compete heavily with Microsoft's Component Object Model (COM, which is the basis of ActiveX) and should be released in late 1996. Until then, however, developers who want to provide inter-applet communication must resort to other methods.

Possibilities of Inter-Applet Communication

There are several interesting possibilities that will be available when applets are truly enabled to communicate among themselves. Here are possible types of communication:
  • Applets that share the same Web page within the same browser
  • Applets loaded in completely different browser windows
  • Applets loaded on different client's browsers
  • Applets loaded from different servers
Using a technology such as Java Beans or ActiveX will also allow developers to "wrap" their applets with code that will expose an applet's methods and properties in some standardized way. Using the component model, the applet could potentially be dropped onto a Visual Basic form or used to communicate with applications written in other programming languages.

Some Inter-Applet Communication Methods

Once again, keep in mind that currently, the only way to implement this type of communication is to take advantage of specific browser features. The following examples focus on capabilities using the Netscape Navigator browser.
Note
Throughout this book, there has been and will be little mention of operating system-specific problems. This is because Java is truly a cross-platform development tool. However, it is interesting to note that there are many browser-specific tendencies. Perhaps those who say the Web browser is an operating system are somewhat correct. This is certainly true in the case of Java programming.

Using the AppletContext


Calling the getApplets()or getApplet() method is the easiest way to reach applets running within the same Web page.

Using a Web Server

A somewhat more difficult, and less reliable, method of inter-applet communication is the use of socket-based communication between applets using the originating Web server. At the current time, any desired type of messaging service can be set up on the server, although it has been mentioned that Sun may discontinue this feature and limit communications to the server's HTTP port only.

Using JavaScript

The Netscape Navigator 3.0 browser implements a technology known as LiveConnect to allow an applet to expose its methods and data to the outside world. Using the scripting language known as JavaScript, the Web page developer can access and change an applet's internal data.
Netscape Navigator 3.0 will also allow the use of cookies. Cookies are basically text files that can contain messages. At the current time, only JavaScript can actually access cookies, but this feature could be useful to script together applets that were loaded at different times.
These features are truly browser-dependent. HTML files containing these features can only be displayed correctly using the Netscape Navigator 3.0 browser.

Using Static Variables

If both applets doing the communication share the same class, a common inter-applet communication mechanism is the use of static variables. Static variables can be thought of as global variables that apply to all instances of the class within an applet. However, the Netscape implementation of this feature allows static variables to be shared within an AppletContext's boundaries. This, then, is yet another method for "passing" data between applets within the same AppletContext. 

No comments:

Post a Comment