Sometimes it is necessary for an applet to find other applets and communicate with them to do a complicated task. This happens when several distinct applets are not enough to accomplish a task. In this section, we shall only explain how communication among applets running within the same browser takes place. There is hardly any occasion where applets running in separate browsers want to communicate. If it is really necessary, they can use various technologies, such as sockets, RMI etc., which have already been discussed in the previous chapter.
The important part of applet communications is finding other applets. For this purpose the AppietContext interface provides two methods getAppiet() and getAppiets().
1. Using getApplet() Method
This method is used to find an applet by its name. It has the following form:
Applet getApplet(String appletName);
The getAppiet() method returns an Applet object with the specified name. The name of an applet is one that is specified by the name attribute of the <applet> tag.
For example, the name of the following applet is server.
<applet code=”Server” width=”200″ height=”80″ name=”server”>
</applet>
Since the getApplet() method returns a generic Applet object, it is typically converted to your custom applet object. Suppose, the name of your applet class is Server and an instance is created with the name “server” (i.e., the value of name attribute of <appiet> tag is “server” as shown above). A reference to this applet can be obtained from another applet Client as follows:
AppletContext ac = getAppletContext();
Server s = (Server) ac.getApplet(“server“);
Once a reference to an applet is obtained, desired methods can be invoked on it, as and when required.
2. Using getApplets() Method
This method is useful when one applet want to find another applet which has no name (i.e., no name attribute is specified in <appiet> tag). The getAppiets() method on the AppletContext interface takes the following form:
Enumeration getApplets();
Essentially, it returns a list of all (including itself) applets available in the same context as this applet. In the following code, e is a list of applets.
AppletContext ac = getAppletContext();
Enumeration e = ac.getApplets();
The finder applet can now iterate through the list to get individual applets. Once the finder, gets references to those applets, it can also invoke methods on them. The following example shows how to find other applets.
//GetAppletDemo.java import java.awt.*; import java.applet.*;
public class GetAppletsDemo extends Applet {
String id;
public void init() {
id = getParameter(”id”);
}
public void paint(Graphics g) {
g.drawString(”This is applet”+id, 20, 20);
if(id.equals(”1”)) {
setBackground(new Color(238,238,238));
AppletContext ac = getAppletContext();
java.util.Enumeration e = ac.getApplets();
g.drawString(”Found following applets:”, 20, 40);
int count=1;
while(e.hasMoreElements()) {
GetAppletsDemo a = (GetAppletsDemo)e.nextElement();
String result = ’’applet’s class name = ”+a.getClass().getName()+”,
id = ”+a.id;
g.drawString(result, 20, 40+count*20);
count++;
}
g.drawString(”Found ”+count+”applets”, 20, 40+count*40);
}
else {
setBackground(new Color(170,170,170));
}
}
}
The following applet generates the output shown in Figure 16.17:
<applet code=”GetAppletsDemo” width=”500” height=”40”>
</applet>
3. A Sample Application
Now, let us develop a simple but elegant application that demonstrates how two applets communicate with each other. In this application, the applet Client sends an integer to another applet Server. The Server applet simply returns the square of the number. Finally, Client displays the result. The source code for the Server applet (server.java) is as follows.
//Server.java
public class Server extends Applet {
int val = 1,
result = 0;
public void paint(java.awt.Graphics g) {
setBackground(java.awt.Color.LIGHT GRAY);
g.drawString(“Server”, 20, 20);
g.drawString(“received :” + String.valueOf(val), 20, 40);
g.drawString(“Sent :” + String.valueOf(result), 20, 60);
}
public int sqr(int n) {
val = n;
result = n * n;
repaint();
return result;
}
}
The Server applet has a public method sqr(), which takes an integer and returns its square. The Client applet will use this method to send an integer and get the result. The source code for the Client applet (Client.java) is as follows:
//Client.java import java.awt.*;
import java.applet.*;
public class Client extends Applet {
int n = 2, result;
Server s;
public void init() {
String param = getParameter(“calculator”);
AppletContext ac = getAppletContext();
Server s = (Server)ac.getApplet(param);
result = s.sqr(n);
public void paint(Graphics g) {
setBackground(Color.LIGHT_GRAY);
g.drawString(“Client”, 20,20);
g.drawString(“Sent: \n”+String.valueOf(n), 20,40);
g.drawString(“Recived: “+String.valueOf(result), 20,60);
}
}
Compile these two files and create an HTML file containing the applet declaration as follows:
<applet code=”Server” width=”200″ height=”80″ name=”server”>
</applet>
<applet code=”Client” width=”200″ height=”80″ name=”client”>
<param name=”calculator” value=”server”>
</applet>
Create a Server applet first, with the name “server”. Now, create a Client applet and pass the parameter calculator with the value “server”. The Client applet first gets the value of the parameter. It then finds the applet having this name. If you open this HTML document in a browser, it appears as shown in Figure 16.18:
Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.