When Framework is Overkill

2009/02/03

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.

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

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:

Source code.


Comments