|Table of Contents|
An applet can communicate with other programs in three ways:
This lesson discusses and gives examples of each kind of applet communication.
- By invoking public methods of other applets on the same page (subject to security restrictions).
- By using API defined in the java.applet package, which lets it communicate in a limited way with the browser or applet viewer that contains it.
- By using API defined in the java.net package to communicate over the network with other programs. The other programs must be running on the host that the applet originated from.
Sending Messages to Other Applets on the Same PageUsing the AppletContext
getApplets()methods, and applet can get the Applet objects for other applets running on the same page. Once an applet has another's Applet object, the applet can send messages to the Applet object.
Communicating with the BrowserVarious Applet and AppletContext methods provide limited communication between the applet and the browser or applet viewer it runs in. The most interesting are probably the AppletContext
showDocument()methods, which let an applet tell its browser which URL to display.
Working with a Server-Side ApplicationApplets can use networking features just as any Java program can, with the restriction that all communication must be with the host that delivered the applet to its current host. This section presents an applet version of the example from Writing a Datagram Client and Server.
Using a Server to Work Around Security RestrictionsOne use of server-side applications is to get around applet security restrictions. For example, since applets can't perform file I/O but applications can, you could have an applet save state by forwarding it to a server-side application. The application could then save the state to a file on its own host. As another example, applets originating from the same host but running on different machines can talk to each other using a server-side application as an intermediary.
This section presents an example of getting applets running on different hosts to talk to each other. It uses sockets for communication between each applet and the server.
|Table of Contents|