Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

How to develop commercial-quality Java programs

By exploring infoBook, a customizable, open Java app, this successful developer shows you how to build your own commercial product

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
When the folks at Sun released the Java Development Kit (JDK) last year, programmers from all walks of life decided to try their hand at developing applets and applications using the new language. Just visit the Gamelan and Java Applet Rating Service (JARS) sites, and you'll see what I mean. However, many of the applets available on the Web are not necessarily the best models for developing Java applets and applications, nor do they take full advantage of the Java language.

In this article I'll provide some guidelines for developing commercial-quality applets, along with some tips on how to package and market your Java applet or application. I use my applet infoBook as an example -- mainly because it has an open architecture. It can be used as a product catalog, phone book, real estate lister, or whatever else you can think of. Additionally, it can be connected to any data source such as RDBMS, X.500, or flat files.

Before you begin coding, it is best to spend some time designing your application, as a well-designed application will most likely result in a product with a solid architecture once it has been built.

Applets versus applications

One key point for consideration is whether you want to develop an applet, which runs inside a Web browser, a standalone application, which can be invoked similar to Microsoft Windows and X-Window programs (for example, Notepad and Xterm, respectively), or both. It is actually easy to develop what I call an appletation -- something that can run as an applet or as a standalone application. InfoBook is designed to run either way. To achieve this flexibility, a bit of custom coding is required for each mode -- applet or application -- due to the particular capabilities and restrictions in both.

For security reasons, applets cannot read or write from or to a user's local disk. Applications cannot perform tasks such as playing audio files or linking to URLs in a Web browser but can access a local disk, execute local programs, and much more. To adapt to the modes of either applet or application, you simply have to use some sort of flag to indicate either the applet or application mode your appletation is running in. This flag -- say a boolean variable named inAnApplet -- can be set to a default value of true. If the appletation is invoked as a standalone application, then this flag can be set to false in the main() method. From there on, before executing some applet- or application-specific code, you can check this flag to determine what to do.

Listing 1 shows an example of how infoBook uses the flag concept to access its data from a local disk using the FileInputStream class or from a remote host using the URL classes.

DataInputStream dis=null;
if (!inAnApplet)
{
    FileInputStream fis=new FileInputStream(dataSource);
    dis=new DataInputStream(fis);
}
else
{
    java.net.URL urlDataSource=new URL(applet.getCodeBase(),
                                       dataSource);
    dis=new DataInputStream(urlDataSource.openStream());
}


Listing 1: Applet versus application logic flow

Applets: Embedded versus framed

There are two modes for invoking applets. The first one, which is more common, is what I call an embedded applet (shown here) as it becomes part of an HTML page.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources