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

An Illustration of These Techniques

The following code will place a frame on a web page, and then save its position as it is moved, so that it appears at the altered position when a new page is loaded. This demonstrates persistence.

An Applet Example

import java.awt.*;
import java.applet.*;

public class Applet1 extends Applet{
  public void init(){
    if(Constants.frame == null){
      Constants.frame = new Frame();
      Constants.frame.reshape((int)Constants.framex.intValue(),
      	(int)Constants.framey.intValue(),300,300);
    }
    Constants.frame.show();
  }

  public void stop(){
    Constants.framex = new Integer(Constants.frame.bounds().x);
    Constants.framey = new Integer(Constants.frame.bounds().y);
    Constants.frame.dispose();
    Constants.frame = null;
    }
}

abstract class Constants{
  static Frame frame;
  static Integer framex = new Integer(225);
  static Integer framey = new Integer(150);
}

Compiling this code will generate the two files Applet1.class and Constants.class.

Place the following code in HTML Page 1:

<HTML>
<HEAD>
</HEAD>
<BODY>
	<applet code=Applet1.class name=Applet1 width=1 height=1> </applet>
<A HREF="Page2.htm">Page2.htm</A>
</BODY>
</HTML>

Place the following code in HTML Page 2:

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
	<applet code=Applet1.class name=Applet1 width=1 height=1 id=Applet1> </applet>
<A HREF="Page1.htm">Page1.htm</A>
</BODY>
</HTML>

When the applet is loaded, method init() is called. This creates a frame, and places it at the (x,y) position defined by variables framex and framey. A look at class Constants tells us that these are initialized at 225 and 150 respectively. Suppose that while Page1 is up, the user moves the frame. Method stop() is called automatically when Page1 is left by the user, and this determines the (x,y) position to which the frame was moved by the user, and remembers it by changing variables framex and framey. When the page is reloaded, method init() is called again. It seems obvious that the frame should be placed again at coordinates (225,150), as defined in class Constants, however, the values of variables framex and framey, when a reload occurs, are those set by method stop() when the previous page was left. The initialization in class Constants is ignored by a reload. Why? The program is not being restarted; it is simply being re-activated. That is the essence of persistence as generated by applet reloading.

There will be stability problems with the frame in some browsers, for it is constantly being destroyed and recreated again (our later examples remove this problem). This is associated with the Abstract Windows Toolkit, and is not linked to persistence. The said instability is present even when the applet is not being continually reloaded, and is caused by the fact that the applet must interact with a peer, which does the actual work of constructing the frame. The said instability is removed in three ways: the first is to adjust the order of frame operations so as to minimize pressure on the peer. The second is to follow every frame operation with a call to pack(). This appears to place a modal stop on further execution until the peer has completed its task. The third is to introduce delay loops at critical points. For instance, one might define the method pause(200), in which pause(time) is defined as follows:

void pause(int time){
  long startTime = System.currentTimeMillis();
  while(System.currentTimeMillis() < startTime + time){
    //infinite loop: time slicing creates a pause to give the peer an opportunity to finish
  }

Notice in the applet code that the frame name is defined in the abstract class Constants. This is absolutely essential for stability. Notice also that the class Constants is never instantiated. You can see already, in this example, that we have started to manipulate an abstract class.


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