Skip to main content

Programming Hamlets

Separate content from presentation with this servlet-based framework

René Pawlitzek (rpa@zurich.ibm.com), Research and Development Engineer, IBM
Photo of Rene Pawlitzek
René Pawlitzek is a citizen of Liechtenstein and holds an engineering degree in computer science from the Swiss Federal Institute of Technology (ETH Zürich). René works as a research and development engineer focusing on security information management solutions for the Advanced Operating Environment group (formerly Global Security Analysis Lab (GSAL)) at the IBM Zürich Research Laboratory in Switzerland. Before coming to IBM, he worked in California for Hewlett-Packard, WindRiver Systems, and Borland International.

Summary:  See various aspects of Hamlet programming through a number of practical Hamlet examples. The examples are part of WebZEC (Web-based Zurich Event Console) -- a fast, browser-based console to quickly navigate in intrusion-detection alarms. With these samples, you can develop a good understanding of how to use Hamlets for Web-based application development and how Hamlets work.

Date:  13 Mar 2007 (Published 24 May 2005)
Level:  Intermediate PDF:  A4 and Letter (1131 KB | 103 pages)Get Adobe® Reader®
Activity:  171 views
Comments:  

Introduction

About Hamlets

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
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).


About this tutorial

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.

1 of 13 | Next

Comments



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development, Open source
ArticleID=464891
TutorialTitle=Programming Hamlets
publish-date=03132007
author1-email=rpa@zurich.ibm.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers