Using Hamlets with Codenvy

Factory URL:

Hamlets (previously known as IBM Servlet-based Content Creation Framework) is an open source system for generating dynamic web-pages developed by René Pawlitzek at IBM. A Hamlet is a servlet extension that reads XHTML template files containing presentation using SAX (the Simple API for XML) and dynamically adds content on the fly to those places in the template which are marked with special tags and IDs using a small set of callback functions. A template compiler can be used to accelerate 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 Hamlets framework not only supports but also enforces the complete separation of content and presentation. It is the result of a radical software simplification effort.

Create a Project

Login to your Codenvy workspace and create a New WAR Project say “hamlets”. Project file structure is shown below :


Dependencies : 

Specify the below dependencies in pom.xml which are prerequisites. Make sure these jars are included under Maven Dependencies.

GetImage (1)

Unfortunately hamlet.jar isn’t available in Maven repository. So, It is advised to download from and build a maven repo out of it , ZIP and upload to the defined location (as specified in <repository></repository>)

Pom.xml : 

<!-- In Project repository -->
<name>In Project Repo</name>

Create Greet Class

Create a public class, Greet, that extends Hamlet. The Hamlet class is an extension of HttpServlet and inherits the familiar servlet methods: init(), doGet(), doPost(), getServletInfo(), and destroy().

Once a user sends a request for the deployed greet Web application, the Hamlet’s doGet() method is invoked. doGet() needs to respond to the user request by returning an HTML page that shows the greeting.  You’ll pass the name of this template file to the serveDoc() method of the Hamlet framework. In addition to the template, you’ll pass a handler to the serveDoc() method. The handler is an instance of GreetingHandler and is responsible for displaying the greeting.

Code Logic :

package com.codenvy;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.*;
public class Greet extends Hamlet
private static class GreetingHandler extends HamletHandler
private String greeting;
public GreetingHandler(Hamlet hamlet, String greeting)
this.greeting = greeting;
public String getElementReplacement(String id, String name, Attributes atts) throws Exception
if (id.equals("greet"))
return greeting;
return "?";
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException
HamletHandler handler = new GreetingHandler(this, "Hello World!");
serveDoc(req, res, "template.html", handler);
catch (Exception e)
throw new ServletException(e);
public String getServletInfo()
return "Greeting servlet";

Create template.html :

This is the template that’ll be displayed upon servlet request. This is used to generate dynamic content (where <REPLACE> </REPLACE> are identified and replaced) using Handler.

<META HTTP-EQUIV="Refresh" CONTENT="10" />
<TD HEIGHT="30">
#<REPLACE ID="greet">Greeting!</REPLACE>

Create web.xml



Build and Run the application

Upon successful building the application and executing, it displays.