advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

JavaBoutique : Articles : Open Source Shopping Cart:

Contents
Introduction
Persistence: Method 1
Functionality
Persistence: Method 2
Illustration: Example 1: Applets
Illustration: Example 2: Active X
Illustration: Example 3: Pseudo-Constructor
The Code
Extended Examples
Shopping Cart and Database
Checkout
Multiple Merchants
Conclusion
Addendum: Mac Version and Back End Progress

Persistence: Method 2

Let us now look at the second method for developing persistence between HTML pages. Although it uses abstract classes, we will see that this limitation can be bypassed. In nature, we observe objects like robins and crows, but never things such as "birds." Robins and crows are birds, but birds as such do not exist; they are an abstraction. Similarly, abstract classes in JAVA are a tool for dealing with things that are common to various objects (in nature, for example, commonalities would be wings or beaks)-- they are not themselves instantiated. What we are going to do, in general terms, is take an abstract class which is an abstraction of nothing, and inject code into it, just as a virus inserts its DNA into a cell and takes over the machinery for its own purposes. An infected cell turns out copies of the virus; the abstract class, in our case, simply chooses to "live forever." It is all done, as you will see, with completely standard JAVA coding. The principle will then be extended through standard coding, to classes which are not abstract.

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 that applet code is composed solely of pointers to static variables defined in some abstract class (sample code is presented further on). 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() or start() 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 move back and forth from an active to an inactive state, as the user browses from one HTML page to another. 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. It also 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, and the buttons continue to work. JAVA in the frame is fully enabled at all times.

There appears to be a small restriction. If you are running JAVA off-site (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 carrying out programming in a page, then it is theoretically possible to crash the browser. In plain language, suppose that the shopping cart frame breaks free of the launching site and you go to some other point in cyberspace, such as the Mars Lander site, starting some big VRML program. Then, as it is in the middle of calculations, and things are changing on the screen, if you bring up the persistent shopping cart frame and begin pushing buttons, you can probably provoke a crash. However, it takes a good deal of careful manual dexterity.

The solution here is simple: Avoid the use of "new" in any JAVA programming that is triggered when off-site. I have tested this, and it appears to make it impossible for even the most obnoxious person to crash the cart. This step has not been taken in the current code because it can involve the use of global static variables, and these make further program development very difficult. Also, the slight potential instability off-site does not appear to be a problem at all for this shopping cart in every-day usage.


NEXT ->

How to Add Java Applets to Your Site

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.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs