How can we use a servlet as a proxy for communications between two applets?

Jim Garrett

Jim Garrett writes:

One way to accomplish this is to have the applets communicate via TCP/IP sockets to the servlet. The servlet would then use a custom protocol to receive and push information between applets. However, this solution does have firewall problems if the system is to be used over and Internet verses an Intranet.
Alex Chaffee adds:
You could also have the applets use HTTP to communicate with the servlet, using the standard URLConnection classes as described in How can my applet communicate with my servlet?. This is more likely to get through the firewall, but it means that the second (receiver) applet must continually poll the servlet to check for new information.
Jim Garrett continues:
One other solution which works with simple data is to combine Java Applet technology with Java Scripts. Since an applet can communicate with Java Script, and Java Script can communicate with applets, it is possible to create an environment where JavaScript is used to pass information between applets. This solution works when the data being sent between applets is text data.
[ Douglas E Miller had given some source code examples. It has been pointed out to me that this code is actually from the Java Tutorial; you can look at the KnockKnock example at http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html. Of course, the KnockKnockServer isn't actually a servlet, which is kind of important for the sake of the example, but you can probably figure it out from here. In this architecture, the servlet must spawn a thread (probably in its init() method) that opens and listens on a socket. This does not use HTTP at all, though you can extend the doGet() method to provide a status page. That means it's still sensitive to firewalls. -Alex ]