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 3

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


Communicating with a Server, AJAX Style

Like a typical browser-based application, a Webtop application communicates with a central server to provide shared services, such as database access. When creating a browser-based application, you have two fundamental choices for server communication:
  1. Access the data via the middle tier and send HTML back to the browser
  2. Access the data from the client browser using AJAX and retrieve XML, which is then formatted into the browser using JavaScript on the client

AJAX has recently found favor among developers because it eliminates the need for frequent full page refreshes, enabling a more seamless and dynamic GUI. Nevertheless, the AJAX approach has disadvantages too—chief among them is the heavy reliance on JavaScript and its concomitant weaknesses. For example, to communicate via AJAX you write JavaScript like this:

if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");

Notice that each browser requires its own special code conditionalized by if statements. This, in fact, reflects the greatest difficulty with browser-based applications—for all but the simplest applications, you must write special code to manage incompatibilities and differences between all of the potential browsers that may exist on the user's desktop.

Your Webtop will use AJAX-style communications via Java. Web Start provides advantages here, too. Namely, in a corporate environment most users access the Internet through a proxy server. Using standard Java, communication through a proxy is not difficult. However, it requires a priori knowledge of the proxy server: the IP address and port it uses. You must provide this information to the Java runtime environment (JRE) either via the launching command line, for example:

java -Dhttp.proxyHost=myProxyServer.com -Dhttp.proxyPort=80 MyJavaApp

Or by setting system properties:

Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost", "myProxyServer.com");
systemSettings.put("http.proxyPort", "80");

When Java Web Start launches an application, it uses a special JRE (javaws) that automatically acquires proxy information, typically from the user's browser settings, and applies the information as necessary. Thus, the developer doesn't need to require the user to discover and enter the proxy information into the application.

Sending and retrieving information AJAX-style from the server via HTTP is easy, as Listing 2 shows. In fact, standard Java SE includes a full suite of classes for implementing AJAX-style HTTP communications with a servlet.

Listing 2. Server Communications, AJAX-Style

public static String sendServletMsg(String content, URL url) { String response = ""; // URL connection channel. URLConnection urlConn; try { urlConn = url.openConnection(); // Indicate we want input. urlConn.setDoInput(true); // And we want to do output. urlConn.setDoOutput(true); // No caching urlConn.setUseCaches(false); // Set the content type. urlConn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); // Send POST output. DataOutputStream printout = new DataOutputStream(urlConn.getOutputStream()); printout.writeBytes(content); printout.flush(); printout.close(); // Get response data. BufferedReader input = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); String str; while (null != ((str = input.readLine()))) { response += str; } } catch (IOException e) { log.warning("IO Exception in ServletUtils: " + e); } return response; }

The code in Listing 2 is actually synchronous, but you can make it asynchronous by simply executing the method in its own thread, allowing the user to continue to work with other features in the application.

The Richly Featured Desktop Client Comes Back

Most existing Java applications can be easily ported to run under Java Web Start, simply by creating a .jnlp file and signing and copying the requisite files to the server. JDIC puts Java applications on equal footing with typical desktop applications by providing access to native desktop features. Together, these powerful tools could lead to a new generation of Webtop applications and the reemergence of richly featured desktop clients.

Michael Bangham is president of Mammoth Software LLC. He has more than 20 years of programming experience and has been programming primarily in Java and C/C++ for the past 10 years.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date