DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. When Framework is Overkill

When Framework is Overkill

Dapeng Liu user avatar by
Dapeng Liu
·
Feb. 03, 09 · Interview
Like (0)
Save
Tweet
Share
8.92K Views

Join the DZone community and get the full member experience.

Join For Free

Recently there have been some hot articles about 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.

Template:

<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:

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

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ...
    }

    @Override
    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());
            response.setContentLength(buffer.length);
            response.getOutputStream().write(buffer);
        }

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

    ...
}

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

Source code.

Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Secure Your CI/CD Pipeline
  • Select ChatGPT From SQL? You Bet!
  • 5 Factors When Selecting a Database
  • Top Three Docker Alternatives To Consider

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: