RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Webtops: The Best of Desktop and Browser-Based Apps in One : Page 2

Use Java Web Start, JDIC, and AJAX-style communications to create powerful, rich desktop clients called Webtops.


Web Start and JNLP

A successful Webtop application must eliminate the installation process—at least from the user's perspective—and provide the means to seamlessly deploy code updates to the user's machine. Of course, every solution requires that some code exist on the user's machine, and it must arrive there somehow. Browser-based solutions deliver code to the desktop via several channels, including the code in the browser itself, HTML, dynamic HTML, and plug-ins. For a Webtop, Web Start/JNLP is the solution.

Sun Microsystems introduced the Java Web Start framework with little fanfare in 2001. It provides the most essential feature for a Webtop: the application can be installed and launched directly from a simple hyperlink and updated automatically without user intervention. Web Start takes care of the details, automatically downloading and caching the application's jar files, updating jar files when needed, offering to provide desktop shortcuts, and handling communication details such as communication through a proxy server.

Web Start applications provide the same level of security to the user as a browser-based application. Like an applet, Web Start applications by default run in a sandbox. If the user trusts the application, however, the sandbox restrictions are lifted during installation and the application can run with the same privileges as any trusted desktop application.

JNLP is the protocol Web Start uses, although the terms 'Web Start' and 'JNLP' are often used synonymously. Most Java applications can be easily converted to run under Web Start by simply creating a .jnlp file and copying it, along with the application's jar files, to the server. Then, you can create an HTML page with a hyperlink pointing to the .jnlp file and your Web Start application will be ready to go.

The .jnlp file defines the characteristics of the application, such as the following:

  • Required jar files
  • Paths to the jar files
  • Whether the application can be used off-line
  • Whether shortcuts should be defined

Creating a .jnlp file is easy. A JNLP file typically is encoded in XML and includes sections for application information, security, and external resources (see Listing 1 for an example).

Listing 1. Typical JNLP File

<?xml version="1.0" encoding="utf-8"?> <!-- JNLP File for MyProjects Application --> <jnlp spec="1.0+" codebase="http://www.mammothsoftware.com/v2/" href="myprojects.jnlp"> <information> <title>MyProjects</title> <vendor>Mammoth Software LLC</vendor> <homepage href="../MyProjects.html"/> <description>Project Manager Application</description> <description kind="short">Project Manager Application</description> <icon kind="splash" href="images/MyProjectsSplash.gif" width="509" height="400" /> <icon href="images/mammoth.gif"/> <shortcut online="false"> <desktop/> <menu submenu="MyData"/> </shortcut> <offline-allowed/> </information> <security> <all-permissions/> </security> <resources> <property name="webStarted" value="true" /> </resources> <resources> <j2se version="1.5"/> <jar href="MyProjects.jar"/> <jar href="MyData.jar"/> </resources> <resources os="Windows"> <jar href="windows/jdic.jar"/> <nativelib href="windows/jdic_stub.jar"/> <nativelib href="windows/jdic-native.jar"/> </resource> <application-desc main class="com.mammothsoftware.app.myprojects.MyProjects2_0"/> </jnlp>

All information for the .jnlp file is contained within the <jnlp> tag. All URLs within the <jnlp> tag are relative to the codebase attribute. The <information> tag provides some basic information about the application: description, location of a custom splash screen, and shortcut information. The <offline-allowed> tag, when included, allows the application to be used off line.

The <security> tag is key; when it contains the <all-permissions/> child tag then the application will ask the user for full access to the user's machine. Therefore, the application's jar files must be signed with a certificate.

The <resources> tag provides a list of the resources the application requires, such as jar files and native files. Notice the references to jdic.jar and Windows native libraries (the next section discusses JDIC).

Lastly, the <application-desc> tag describes the main class used to launch the application.

Once the .jnlp file is ready, you drop the file on your server, place the resource files on the server in their correct location relative to the codebase attribute, and you're ready to go.

One final point: the Web server must be configured to support the JNLP MIME type. This is a simple task: just add the JNLP MIME type to a server configuration file.

Java Desktop Integration Components

Sun introduced the JDIC project in 2004 to address a primary weakness of Swing—its lack of effective integration with the native environment. Swing provides an assortment of pluggable native GUI elements, but they lack any real connection with the desktop environment, such as browser integration, application launch via a double click using the file extension, use of the system tray, etc.

JDIC rectifies this problem by providing an interface to native OS-specific features. Of course, the obvious question is 'what about write once, run anywhere?' Indeed, using JDIC today requires some minor compromises. The application must be deployed using different native libraries for each platform. Fortunately, Java Web Start automatically selects and downloads the proper native libraries to the user's machine—again, without requiring user intervention. And Sun is considering adding JDIC to Java SE, which will ease deployment considerably.

Your Webtop application requires one JDIC component in particular: the WebBrowser, which enables tight browser integration with your Webtop application. You can easily embed the user's installed browser directly into your application with just the following few lines of code:

private WebBrowser webBrowser = new WebBrowser();

private void loadStartingPage() {
    try {
        webBrowser.setURL(new URL("http://www.java.net"));
    } catch (MalformedURLException e) {

Since JDIC provides a wrapper around the user's installed browser, rather than providing a full-fledged browser component, the download is quite small. Even better, the WebBrowser provides a WebBrowserListener that allows your application to listen for and intercept browser events:

import org.jdesktop.jdic.browser.WebBrowserListener;
import org.jdesktop.jdic.browser.WebBrowserEvent;

public class WebBrowserAdapter implements WebBrowserListener {
    public void downloadStarted(WebBrowserEvent e){
        System.out.println("Download Started");
    public void downloadCompleted(WebBrowserEvent e){
        System.out.println("Download Completed");
    public void downloadProgress(WebBrowserEvent e){}
    public void downloadError(WebBrowserEvent e){}
    public void documentCompleted(WebBrowserEvent e){}
    public void titleChange(WebBrowserEvent e){}
    public void statusTextChange(WebBrowserEvent e){}

The dowloadStarted event is especially powerful because you can easily intercept the event and then fire application-specific events. For example, when the user selects a hyperlink, you can cause an application-specific action to occur, such as displaying a menu or executing a desktop-type command. Alternatively, you can let the browser navigate to the selected hyperlink, or even navigate to different links depending on the user type and associated privileges. Thus, your application becomes richly context sensitive.

Figure 1 shows an example Webtop application with the integrated browser. In this example, when the user selects the link labeled "Switch to the Main View" the application opens a new data file and navigates to the data management portion of the application, shown in Figure 2.

Click to enlarge

Figure 1. Webtop Application with Integrated Browser

Click to enlarge

Figure 2. Typical Webtop Application Screen

Alternatively, when the user selects the link labeled "Help", the browser navigates to and displays an HTML help page. To implement this effect, you trap the downloadStarted event and examine the requested hyperlink. For certain hyperlinks, you cancel the download and execute a command instead.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date