provided by: 
Originally published at Internet.comEvolution of Web Applications
The web browser has become a primary vehicle for delivering business applications to users. There are several benefits to delivering software through the web, including easy administration and simplified accessibility. For all these benefits, however, web applications typically exhibit limited functionality when compared to thick client software. Not only is usability diminished, developing these web applications is often more complex than writing for the desktop, mainly due to the stateless nature of HTTP. Not long after the advent of the web, developers began seeking ways to make their web sites mimic the look and responsiveness of desktop applications. This was often be a dangerous proposition because doing so typically required complex client side programming with JavaScript. Recently, the promise of Ajax technology has given web developers new hope. This article will introduce the open-source Ajax framework known as ZK that allows Java developers to create rich web applications.
Ajax (Asynchronous JavaScript and XML) technology relies on asynchronous communication between client and server to provide users with a dynamic and responsive interface. In traditional Java Servlet-based web applications, a page's entire content is sent via a request to the server where a response is generated and returned. When the client receives this response, the complete page is rendered back to the user. This is the case even if a specific request is relevant to only a small portion of the page. In this scenario, given the amount of processing involved from request to response, contact with the server is kept to a minimum. With Ajax, several light weight requests are continuously sent to the server and responses to those requests cause a partial rendering of the page's model. Server requests are more frequent in nature, but they also tend to be smaller and less expensive. Developing Ajax applications can be a costly enterprise if it means adding significant amounts of JavaScript code. Client script can be difficult to write and maintain, largely due to the fact that different browsers support different implementations of JavaScript and these issues must be addressed by the developer.
ZK Framework Overview
ZK is an AJAX-based web framework from the Potix Corporation. ZK is open-source, licensed under the GPL (GNU Public License). ZK's AJAX engine consists of both client and server components that communicate with one another. ZK provides two sets of user interface components. One set of components is based upon XUL, or the XML User Interface Language. The second set utilizes XHTML. ZK provides an XML-based markup language called ZUML for adding components to a page. Though ZK's Ajax engine makes considerable use of JavaScript, it is important to note that the complexity of implementing Ajax is concealed from the developer. Client side events send data to the server where the developer can handle them with Java code. ZK provides a simplified programming model, similar in many respects to programming with Java Swing. In the Model-View-Controller architecture, ZK addresses the 'View' portion of an application. Developers are free to use technologies such as Enterprise JavaBeans and object-relational frameworks such as Hibernate in conjunction with ZK. It is even possible to incorporate ZK into applications that use view technologies such as JavaServer Pages and JavaServer Faces.
In the remainder of this article, we will take a brief look at ZK in action. The sample application presented here is by no means a comprehensive demonstration of ZK's features. Hopefully, it will encourage you to explore the framework yourself in greater detail.
ZK in Action
The sample application allows university students to create a schedule of courses. Thhere are several domain objects used to represent data used by the application: Java Class Description Course Subject offered at university Room Room on university campus Faculty Instructor at university AvailableCourse Course that student may schedule Student Student at university StudentCourse Course in student's schedule
An AvailableCourse is an aggregate of Course, Room, and Faculty. It represents a specific course offering for the current semester. A StudentCourse links a Student instance with a particular AvailableCourse. A collection of StudentCourse objects will comprise a student's schedule. I used the NetBeans IDE to develop this sample application. In addition to the domain classes mentioned above, I created a simple Data Access Object in Dao.java that event handlers can use to access sample data. You can download this sample application, including the source code and .war file, here. All .jar files necessary for building ZK applications are included in the ZK distribution. After adding these libraries to your project, you will need to modify the web.xml configuration file to add Servlet mappings that allow ZK to process requests. The ZK download includes a demo application and the easiest way to get started on a new application is to copy its web.xml file and customize as necessary.
Start Page
As mentioned earlier, ZK provides two different component sets, XUL or XHTML. You can actually use both in the same page using XML namespaces. This application will utilize XUL components. ZUML (ZK User Interface Markup Language) is used to specify page components. With ZUML, you can arrange the layout of components on a page, modify attributes for these components, and wire component events to event handlers. It is also possible to handle events directly in the page using the element. Zscript lets developers embed Java code in a page, similar to Java scriptlets in JavaServer Pages. Of course, the more robust option is to handle events inside Java classes but we will take a look at both options.
Index.zul contains all the components used in the sample application. You have the option to break up the interface into smaller, reusable files and include them in the main page as you might do with the include directive in JavaServer Pages. The component is a top level component that will normally encapsulate several components on a given page. Like all components, you can modify a window's attributes to modify its appearance and name event handlers. A convenient way to handle component events is to extend the component class. This 'use' attribute in ZUML enables you to specify a custom subclass to represent the component. As an
If you prefer in certain situations to handle event logic directly in your ZUML page, you can easily do so with the element. There are virtually no limitations to what you can do in the zscript portion of a page. As an example, look at the following toolbar component for the main window:
The onClick event method in this case will be written within a element. The particular event handler displays a popup window with contact information for questions about student enrollment. Before it displays the window, however, it programmatically creates components and adds them to the window: void showContacts() { Label primaryContact = new Label(); primaryContact .value="Primary: John Smith - (502) 545-1900"; Label secondaryContact = new Label(); secondaryContact .value="Secondary: Julie Stewart - (502) 545-1935"; winAbout.appendChild(primaryContact ); winAbout.appendChild(new Separator()); winAbout.appendChild(secondaryContact); //Display window on top of main window. winAbout.doOverlapped(); }
The window that displays contact information is also defined in index.zul:
As previously mentioned, you can add several components and even several windows filled with child components in a single ZUML file, or you can break them up into separate files. Test the sample application and witness some of the features it provides, message boxes for example. Adding such functionality was trivial but can greatly improve the user experience.
Contacts popup window
Error! Course already appears in schedule
Confirmation before exit
I have only touched upon ZK's numerous features. The ZK site provides excellent documentation. I think you will find it worth your while to experiment with ZK and learn how to build Ajax web applications with minimal effort.
About the Author
Michael Klaene is a Senior Consultant with Sogeti LLC. He has spent over 9 years in Information Technology and is an experienced Systems Analyst, delivering solutions that involve numerous technologies, such as J2EE and .NET.
Author: Michael Klaene
Read article at Internet.com site