Introduction
Hamlets provide an easy-to-use, easy-to-understand, lightweight, small-footprint, servlet-based content creation framework that facilitates the development of Web-based applications. The Hamlet framework not only supports but also enforces the complete separation of content and presentation. Its simple and elegant design does not hide the familiar underlying servlet infrastructure.
A Hamlet is a servlet extension that uses SAX (the Simple API for XML) to read XHTML template files containing presentation; while the XHTML file is being read, the Hamlet uses a small set of callback functions (implemented by a HamletHandler) to dynamically add content on the fly to those places in the template that are marked with special tags and IDs. This is illustrated in Figure 1.
Figure 1. A Hamlet uses SAX for reading content from a template file and calls a HamletHandler to add dynamic content
Hamlets should be used in combination with other Web technologies. An ideal blend for a Hamlet-based Web application consists of a number of components:
- Hamlets written in the Java™ language provide the code that is responsible for creating the dynamic content.
- XHTML template files contain the basic layout of dynamic Web pages and thus provide the presentation.
- Cascading Style Sheets (CSS) give Web applications a consistent look and feel.
The project name Hamlets used in this tutorial refers to an internal project for development of a servlet-based framework for separation of content and presentation in Web-based applications. For a more thorough introduction to Hamlets and additional information, you can read my previous developerWorks articles "Introducing Hamlets," "Implementing Hamlets," and "Compiling Hamlets" (see Resources for links).
This tutorial aims to illustrate various aspects of Hamlet programming by providing a number of practical Hamlet examples. The samples should give you a good understanding of how Hamlets are used for Web-based application development and how Hamlets work. In the first example, Clock example, I will draw parallels to standard Windows®-based C or C++ development to illustrate how Hamlets make Web-based development easy.
The code for both the Clock Example and the subsequent examples is part of the Web-based Zurich Event Console (WebZEC), a browser-based application for monitoring intrusion detection events. WebZEC provides an intuitive graphical user interface for security analysts to monitor and investigate intrusion alarms. Its front end was built solely with Hamlets in a very short time frame.
Prerequisites and notes on the sample code
This tutorial assumes that the reader has experience with HTML or XHTML; thus, the XHTML code used in the examples is not explained. It is also assumed that the reader is familiar with the Java language and the concepts of object-oriented programming. Hamlets are servlet extensions, and therefore a good understanding of Java servlets is essential.
The examples in this tutorial were built with the Apache Ant build utility and deployed on the Apache Tomcat servlet container. They require the hamlet.jar library, which is the core of the Hamlet framework. You can find the complete source code that makes up this library in Appendix A. The Hamlet code is listed in this tutorial for your edification.
The two files in Appendix B -- ContextProperties.java and Utilities.java -- contain functionality that is used by the examples. These files are not really necessary to program Hamlets; however, they make life a lot easier. They could be included with a Web-based application or they could be put into a library (such as util.jar) for reuse. It is important to note that Utilities.java depends on mail.jar, the JavaMail API.






