• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

How do I pass variables between two applets on differnt html pages


I would like to know how to pass objects between two applets on differnt
html pages.

I tried using static variables and public static methods. It works only
for Netscape. It doesn't work for IE.

Can anyone help me on this.

Please cc your answers to sswami@krdl.org.sg

ThanX a lot
Cheers swamy
1 Solution
A good way to do what you need is using Piped Streams.
One applet creates BOTH a PipedInputStream and a PipedOutputStream, and gives one end to the second applet.
The applet that has the Output end of the stream can start writing data into it, and the other applet, with the Input end,  can start reading.
In the book 'Hacking Java', which can be found online at www.mcp.com , there is a chapter called 'Inter-Applet Communication'. It has all the information you need, plus some examples.

-- Roy.
sswamiAuthor Commented:
Hi Roy,

I am not sure about the Piped Stream beacuse of the nature of my problem.

I have an html page with an applet A.

The applet A uses url object to open another html page(appletcontext.showDocument()).
This html page contains an applet B.

How can I access a staic var C in Applet A which is set when A is started.


Another way to do it is to use Sockets, but this is one hell of a workaround! May not be feasible, but here is my two cents worth...  :)

If applets are running on the same machine

1) Write a Server on the host machine which listens on two ports, one for applet A, another for applet B. (different port numbers of course)
2) program logic is as follows:
a) Applet A runs first. Opens connection with the Server. Writes the C value into the Server memory.
b) Applet A calls Applet B.
c) Applet B captures Applet A's IP address. B Opens connection with the Server. Reads the C value into the Applet.

Of course its more complex than that (synchronization problems and such), but that's the gist of it.

If the Applets are run from different machines, two Server programs must be run. One on Applet B's host and one on Applet A's host. This is neccesary as not to invoke any security exceptions.

have fun.
One possible solution that I've used is to use a server side script to generate the html page that the second applet is on, attaching a name/value pair to the URL string in the first applet.
   Say you want to pass a userName variable to the applet. Set the URL string to "www.domain.com/applet2.html?userName=" + userName.
   Retrieve the variable in the same way as if you had an html form field named userName and the write out the information needed to display the applet along with a parameter to pass the userName value on
Another way to get the info from the name/value pairs into the second applet without using Serverside scripts-
 Use the Applet.getDocumentBase() to retrieve the URL of the page the applet is on, which will include the name/value pair you attached to the URL object from the first Applet.
 Note: neither solution will work with an object, but you can pass the information you need to recreate the object in the second applet.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now