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.
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());
}
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.