by John D. Mitchell

Java Tip 3: Our first Java Tip winner!

how-to
Mar 1, 19962 mins

How two applets on different Web pages communicate

This week we present our first Java Tip of the Week Contest Winner! Our Java Tip this week comes to us from Greg Williams.

Greg has put together some code to show us one way for two applets on two separate pages to communicate with each other.

The Ticket Frames page will launch the demonstration. Two frames will be displayed with each frame displaying a different HTML page. The Ticket Holder page will display in the top frame while the Ticket Reader page displays in the bottom frame. Note that you will need Netscape Navigator 2.x or some other browser that supports HTML frames to view this demonstration.

Those two simple pages are just HTML wrappers for running the TicketHolder and TicketReader Java applets, respectively. Both applets make use of the Ticket abstract Java class.

The two applets are straightforward examples of multithreaded Java applications. The pair of them implement a classic reader/writer scenario. The TicketHolder class is periodically updating the shared data while the TicketReader class is periodically reading the shared data.

The โ€œshared dataโ€ is encapsulated in the Ticket class as a โ€œstaticโ€ data member manipulated by the synchronized, static accessor and mutator class methods. So each applet is just using the Ticket class as a third party to facilitate communication. This method will scale up to handle more readers (but multiple writers offer lots of fun new problems).