Cracking the HTML Persistence Barrier
If one looks at abstract classes in JAVA, one notices that they are tied very closely to the JAVA virtual
machine. If they could be instantiated, then certainly they would persist – programs could then be
remembered along with data, even when the applet was inactive. However, abstract classes cannot
contain constructors. Suppose, however (sample code is presented further on), that applet code is composed
solely of pointers to static variables defined in some abstract class. Suppose further that static components
for a frame (buttons, etc.), as well as the static name of the frame itself, are also defined in the abstract
class.
Then, suppose that init() in the applet calls a static function in the abstract class which instantiates the
static frame and assigns the static components to it. It then sets a flag in the abstract class that tells it not to
do this again. One now has a kind of pseudo-constructor for the abstract class. The resulting frame, and its
components, are tied, through the abstract class, to the JAVA virtual machine itself, and they persist from
HTML page to HTML page, even when the applet is not being loaded. The applet that originally
launched both the frame and the abstract class may itself move back and forth from an active to an inactive
state, as the user browses from one HTML page to another, but this makes no difference to the frame itself,
for it is no longer dependent upon the applet.
Now, how do we enable events within this 'terminate and stay resident' frame – so that it will carry out
actions when 'off site'? It is done in the following manner (sample code follows). Events must be caught in
the frame itself, but all variables and interrupt methods are contained as static variables and static methods
in the abstract class. The frame event handlers simply make jumps to these static variables and methods.
Through a use of these two separate persistence techniques – loading a program repeatedly, and forming a
pseudo-constructor for an abstract class - we have now created a program that lives beyond the page in
which it was launched, remembers data that is collected from page to page for as long as a page accesses
the launching applet through a relative call, and carries out event handling within the frame itself, on applet
data, when off-site.
The JAVA program has now broken away from the launching page, and it ‘lives forever.’ It is interesting to
reduce the browser to a partial window, and to place the shopping cart frame on the desktop beside it.
Browse from page to page, and watch the frame. It doesn’t flicker. The buttons continue to work. JAVA in
the frame is fully enabled at all times. As we will see later, it is possible, in some browsers, to break into
the HTML, from the frame, and to interact with JavaScript on every subsequent page.
There is one restriction. If you are running JAVA off-site – that is, if you force the cart to do something by
pressing a button, and if the resulting event-handling uses ‘new’ to create a variable, and if at the same
time the browser is working very hard at loading a page, then it is theoretically possible to crash the
browser. The solution is simple: avoid the use of ‘new’ when off-site. (This step has not yet been taken
with the shopping cart program because in part it involves the use of global static variables, and these make
further program development very difficult.)
Next ->
Lane Friesen
lanelise@dowco.com
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|