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 :

ResReader

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 http://sourceforge.net/projects/hamlets/files/latest/download?source=files and build a maven repo out of it , ZIP and upload to the defined location (as specified in <repository></repository>)

Pom.xml : 

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>hamlets</groupId>
<artifactId>hamletsJar</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<repositories>
<!-- In Project repository -->
<repository>
<id>hamlet-jar</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/src/main/webapp/WEB-INF/lib/</url>
</repository>
</repositories>

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 com.ibm.hamlet.*;
import org.xml.sax.*;
public class Greet extends Hamlet
{
private static class GreetingHandler extends HamletHandler
{
private String greeting;
public GreetingHandler(Hamlet hamlet, String greeting)
{
super(hamlet);
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
{
try
{
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.


<HTML>
<HEAD>
<META HTTP-EQUIV="Refresh" CONTENT="10" />
<TITLE>Codenvy-Hamlets</TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD HEIGHT="30">
#<REPLACE ID="greet">Greeting!</REPLACE>
#
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

Create web.xml


<web-app>
<welcome-file-list>
<welcome-file>Greet.html</welcome-file>
</welcome-file-list>
<display-name>Hamlet_Codenvy</display-name>

<servlet>
<servlet-name>
Greet
</servlet-name>
<servlet-class>
com.codenvy.Greet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
Greet
</servlet-name>
<url-pattern>
/Greet.html
</url-pattern>
</servlet-mapping>
</web-app>

Build and Run the application

Upon successful building the application and executing, it displays.

GetImage