Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Java Tip 3: Our first Java Tip winner!

How two applets on different Web pages communicate

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
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).

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources