|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java Using Applets in Java Applications
Using Applets in Java Applications
By: Deepinder Sidhu
Feb. 1, 1996 12:00 AM
Java programs can be classified as either Java applications or Java applets, based on the modules' execution content. While applets require the presence of a Java-enabled browser (or an AppletViewer), applications are designed to run on the client machine on the top of the Java interpreter, without the need for special helper applications. Separating applets and applications into two disjoined sets creates inefficiency in software design, since programs designed to enrich browser pages are usually not part of Java applications. In this article, we propose a method to include applets as part of applications. We discuss the similarities and differences between the execution of applets and applications, and offer a brief outline on applet design. And we propose a simple solution for adding applets with limited functionality to Java applications. Next, we present a brief discussion on application level resource management, such as caching images, audio clip data, and designing efficient class loader objects. Lastly, we offer a more complete solution by introducing security issues and network image access as well as design and implementation issues of applet stubs and context.
Applets and Applications
Applications Applications using network services, such as sockets, are responsible for implementing and maintaining program security and integrity at the application level. User-level security proves to be adequate for stand-alone applications, since during the execution of such programs, the environment can only exercise the privileges of the user who initiated the execution.
Applets These methods are called by the browser in the specified order. It is impossible to make the call to the start method, if the applet has not been initialized. The diagram in Figure 1 illustrates in steps the execution of Java applets. While init() and destroy() are called only once, at the beginning and the end of the execution respectively, the applet must be ready to respond to start() and stop() at all times. To improve system performance, when threads and network connections are used, these resources should be suspended and/or freed up upon the call to the stop() method.
Using Applets as Panels The applet class in JDK version 1.0 is derived from Panel, which in a few steps derived from Component. This allows the programmer to add an applet to the application as a separate panel, place or paint other components on applets, etc. This section offers a discussion on design and implementation issues of including applets into applications.
Loading Applets
Running the Applet After the start() method returns, the applet is ready to be shown. The last step is to resize the applet to the desired dimensions. Finally, the applet.show() method is called, as suggested in the code segment shown in Listing 2. The call to the validate() method on the last line is necessary to repaint the applet and to ensure that the applet is correctly displayed by the layout manager.
Suspend and Resume Applications can respond to events such as minimize and restore by overriding the handleEvent function of the application's main frame. The code segment in Listing 3 illustrates this procedure.
Other events, such as destroying the window, can be handled in the code fragment in Listing 3 as well. In this particular example, the applications should call the stop() and destroy() methods respectively to ensure the proper execution and cleanup for all applets.
Applet Stubs and Context
Applet Context Because loading images over the network can be time and resource consuming, the application should implement a caching scheme to eliminate redundant loads. This is usually accomplished by using a hashtable of Image objects. When the applet requests an image, the application first searches the hashtable to see if the specified image has already been loaded. If not, it must make the necessary URL connection to load the image data across the network, otherwise a reference to the hashtable entry is returned. The code in Listing 4 demonstrates this implementation of application level caching. Audio data and other resources can be handled in the same manner. To offer improved performance, the application should also implement a security verification algorithm, that ensures the image in the cache actually contains the same data as the URL source.
Applet Stubs Because applications most often receive their parameters from either the command line or from an initialization file, these input sources must be parsed and the appropriate parameters and values must be passed onto the applets. The code segment in Listing 5 offers one of many implementations of passing parameters from the application to embedded applets, using a system-wide hash table, that contains the parameter information.
Applet Security Installing a new security manager; however, will affect the functionality of the entire application. Every network connection can and should be verified by the application, and possibly rejected if the remote site is classified as untrustworthy. The design and implementation of the SecurityManager is a complex task and this topic is currently under study.
Conclusion
Acknowledgments
Related Publications Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||