Time Efficiency – Controlling Java applet from Javascript especially if you make round-trips to the web page from applet

My application architecture is like this,  for the easiness I will say that there is a clock on my page which a java applet updates every second, that clock shows the server time (in actual my java applet is updating other data as well). This java applet runs continuously once the page get loaded, I am loading it at the end of the page and it is a faceless applet.

Then I have this form for example:

<form id="liveClock">
  <input type="hidden" id="DivID" value="Nothing Yet">
  <input type="hidden" id="DivVal" value="Nothing Yet">

Now my java applet on the page, places the DivID which needs to be updated in DivID element and value of it in DivVal element.

Say after java applet places the DivID and DivVal, these elements look like this:

<form id="liveClock">
  <input type="hidden" id="DivID" value="Clock">
  <input type="hidden" id="DivVal" value="11:02:59">

A javascript function then picks up the data from the DivID and DivVal and updates the Clock div on the HTML page.

<script type="text/javascript">
function updateClock(){
//get the input element data placed by java applet
var DivID = document.getElementById("DivID");
var DivVal = document.getElementById("DivVal");
//now get the clock div
var clock = document.getElementById(DivID);
//check if we really have got the clock div element
if(clock != null){

After placing the data in input elements, java applet calls the updateClock function of javascript, which executes and update the clock on the page. Why applet is not directly updating the div element because applet is also used to update several other elements on the page and each update is coming at once in the form of a comma separated string, explaining all that is beyond the scope of what we are trying to achieve here.

Ok, applet is now running every second, placing data, then calling javascript and javascript is then updating the fields on the html page.  Java on its website says that if you are updating javascript on the web page from Java applet, avoid making round trips, the primary reason is that java applets are multi-threaded while javascript in a browser is single threaded and my applet is indeed a multi-threaded applet, however, I am using one thread explicitly to update the contents on the page while the other one to fetch the update from the back-end.

The problem I was facing that from time to time, whenever my applet is sitting there, sleeping for sometime and then updating the content, if user navigates away from the page, it produces some errors like DivID was not found or updateClock was not defined, while both are there in HTML page.

Root Cause of the Problem

Root cause of the problem was my browser was slow to unload and tear down the java applet while moving to another page but my applet was faster in execution and supposedly its 500ms wait finished while the browser has unloaded the DOM containing DivID element but applet would still try to go and update the element, that will throw errors in the browser.

The Solution:

The solution is simple:

in your HTML page header:

function closeIt()
  document.liveClock.APPLETID.PageLoad = 0;
window.onbeforeunload = closeIt;

where APPLETID is the ID of your applet element on the page. Then in your java applet declare a public integer PageLoadand initialize it with one

public int PageLoad = 1;

Now just when you write to the javascript through doc.eval(string), check the PageLoad variable

if(PageLoad == 1){

So, we are turning off our applet’s writing abilities just when browser is about to unload the page. The onbeforeunload event of javascript is not very famous but becomes very handy in this case.

Hence, we have worked around a problem to which both browser and java wasn’t addressing.

Happy Coding 🙂

Comments are closed.

Get Adobe Flash player