|
by Joe Walker AJAX is the buzzword of the moment among web developers, so much so that you could be sick of introductions to AJAX by now (if that's the case, skip down to "The Chat Web Page"). AJAX is a technology that is hotly debated from many angles, but it has stuck because it encapsulates something that is new from a user's perspective. The functionally that is newly available to all web users is "in-page replacement": the ability for a web page to change using data from a web server without totally redrawing itself. This functionality has been around in Mozilla and Internet Explorer for a while, but it is only recently that Safari and Konqueror users have been able to join in. It is this ability to have web pages that update dynamically that is changing the way users interact with the web. For example:
AJAX isn't the best acronym in the world: it stands for Asynchronous JavaScript and XML. This does nothing to describe the benefits to a user: the technology behind it does not have to be asynchronous, and the best implementations don't necessarily use XML, either. However, the buzzword has stuck so we are better off going with the flow now. The problem for the web developer is that while this is a very attractive way of creating websites, and one where you can get started without a huge amount of effort, there are a number of pitfalls that can make life harder. All browsers have different quirks, so you can easily discover that, for example, you have locked Mac users out of the party. DWR, hosted on java.net, is an Java open source library that helps developers to write websites that include AJAX technology. Its mantra is "Easy Ajax for Java." It allows code in a web browser to use Java functions running on a web server as if they were in the browser. This article demonstrates the use of DWR to create a multi-user web-based chat site. It demonstrates how simple it is to integrate JavaScript in the web browser with Java on the server. The aim is to have a fully functional system in about 100 lines of code for both the client and the server. The emphasis will not be on fancy graphics or lots of chat features, because that would distract us from the core business of how to write AJAX code without lots of effort. The Chat Web PageThe web page has two parts: one area where you can see the messages that others type, and an input field where you can type messages yourself. Figure 1 shows what it looks like.
The HTML is very simple:
We'll come to the JavaScript code in a bit, but let's start with the server side. Just how much code do you need for a multi-user web-based chat system? Server-Side JavaWe have two classes to do the server-side work. The first is the
The constructor does a few simple things: it shortens messages to
256 characters and replaces The other class in the server is the
And that's it for the server-side code! Two of these methods are important from the web browser's point
of view: Configuring DWRNow we need to remote these two methods to the web browser. The first step is to copy dwr.jar into your web app. You can download dwr.jar from its java.net project. Next, you need to configure your app server's web.xml to understand DWR. The standard bit of code looks like this:
Finally, you need to tell DWR about the chat server you just created. Specifically, you need to tell it two things:
DWR could do the second bit for you, but we'll do it this way to
make sure that you don't give away access to anything by mistake.
The DWR configuration file, dwr.xml, is placed alongside
web.xml in your WEB-INF folder. For your chat application,
dwr.xml should look like this (obviously, replace the
We are telling DWR it is OK to create The Client-Side ScriptingThe final bit is the JavaScript that is fired off by the HTML to
call into the Java code. The good news is that DWR makes this bit
easy. Typically, the JavaScript code for this type of thing would
contain complex First we include the JavaScript that tells the browser about the
The script engine.js contains the core of DWR. Generally, you just
include it as is, and then ignore it. There are a few methods in it
that are sometimes useful, but you can see the full
documentation for them at the DWR website. The script util.js
contains some utility functions that are totally optional, but will
help you greatly in getting anything done with DWR. Chat.js
is dynamically generated by DWR as the remote version of
DWR does everything it can to make the JavaScript version of your Java code as simple as possible, but there are some things you need to be aware of. The most obvious is that the "A" in AJAX stands for asynchronous; so by definition, the remote method is not executed the instant your JavaScript code is executed. This would not be an issue, except for the complexity of knowing what to do with the values returned by Java to the browser. DWR solves the problem by asking for a callback method, to which it will pass the returned data. The first parameter to any DWR-generated method is always the callback function. Above, we created a web page with a JavaScript function that
we've not implemented, until now: the
The first line gets the value from the input field.
Next, we use the Then we call
This is where DWR excels. The Java method
And that's it! We have a basic multi-user, web-based chat system in about 100 lines of code, for both the client and server code. There are a number of things missing for this to be useful: a
polling method that uses You can see the final version here. It adds the features listed above, plus highlighting of new messages--all of which takes an extra 50 or so lines of JavaScript. It is also worth checking out http://localhost:8080/[YOUR-WEB-APP]/dwr, which is a test debug page that automatically shows you the classes you have remoted, and allows you to test their functionality. ConclusionUsing DWR can make creating cross-browser AJAX websites very easy, and introduces a very neat way to interact between JavaScript in the web browser and Java on the server. It is simple to get started with and integrates well with your current website. Resources
|
Developing AJAX Applications the Easy Way

