Over a million developers have joined DZone.
Platinum Partner

When Framework is Overkill

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Recently there have been some hot articles about frameworks: Better Java Web Frameworks and Better Java Web Frameworks: ItsNat. Sometimes I've found that frameworks just make life harder. Recently I was assigned a job to create a company's site from scratch. The requirement is relatively simple, less than 10 static HTML pages, literally no server side processing.

I decided to write a simple mini "framework" to aid my client's designer, instead of adpoting an off-the-shelf web framework.

The idea is pretty much the same as most CMS: to keep a big template out and to only let the user (the maintanence designer) touch the content of the page. Yet, I want to provide flexibility to some degree. For example, at least I wanted to do some simple SEO to every page.


<meta name="description" content="{{description}}" />
<div id="head">
... ignored for simplicity...
<div id="content">{{body}}</div>
<div id="footer">... </div>

And with the template, the actual page needs only to provide the information like TITLE, BODY, DESCRIPTION:

e.g., "about-us.html":

{{title}} About Us

{{description}} what ever information that you want search engine know

{{body}} ... here is the real content, ignored for simplicity... 

So I need a ServletFilter to intercept the request and to wrap the response from "about-us.html" and to make any necessary subsititutions.  I search the default servlet's output for anything like {{ABC}} and use "String.replace" to try to replace/insert into the template string. Once all is done, I dump the result to the original response. 

I have included my source code below. The BufferedServletOutputStream class is copy-and-pasted from a Google search result, sorry I couldn't find the author's name:

public class StaticSiteTemplate implements Filter {
private Map defaultParameters = new HashMap();
private String template = "";

public void init(FilterConfig filterConfig) throws ServletException {

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
CapturedHttpResponseWrapper wrapper = new CapturedHttpResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
// find tagString originalContent = wrapper.toString();
Pattern pattern = Pattern.compile( < a
href = "file://%7b//%7B//w+//%7D//%7D"
mce_href = "file://%7b//%7B//w+//%7D//%7D" >\\{\\{\\w +\\}\\}</a >);
Matcher matcher = pattern.matcher(originalContent);
String result = template;
if (matcher.find()) {
ArrayList breakpointList = new ArrayList();
breakpointList.add(new TemplateBreakupPoint(matcher.group(), matcher.end(), originalContent.length()));
while (matcher.find()) {
breakpointList.get(breakpointList.size() - 1).end = matcher.start();
breakpointList.add(new TemplateBreakupPoint(matcher.group(), matcher.end(), originalContent.length()));
for (TemplateBreakupPoint tbp : breakpointList) {
result = result.replace(tbp.key, originalContent.substring(tbp.start, tbp.end).trim());
} else {// assume all belong to bodyresult = template.replace("{{body}}", originalContent);}
// replace default params
for (String key : defaultParameters.keySet()) {
result = result.replace(key, defaultParameters.get(key));
// dump outputbyte[] buffer = result.getBytes(response.getCharacterEncoding());

public void loadTemplate() throws IOException {...}


You can view my site as a live demo of this approach here: http://www.nwhholdings.com 

Source code.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}