Rapid web development

I am experimenting with the following technique to speed up web application development with Java. First, define an interface called Controller with a single method called handleRequest that takes an HttpServletRequest and an HttpServletResponse as arguments. The method should also throw an Exception.

package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Controller {

    public void handleRequest(HttpServletRequest request, 
            HttpServletResponse response) throws Exception;

Then, create a hierarchy of JSP files and folders based on user stories. For example, say, we are writing a page to allow users to register new accounts. We’ll create the following files. In the /user/register folder: – index.jsp This is the entry point for our controller.

  • default.jsp This is the view that is shown by default.
  • success.jsp This is the view that is displayed when user registration is successful.

Now, in index.jsp, write the following:

<% web.Controller c = new web.Controller() {

    public void handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        account = null;

        messages = new java.util.ArrayList();

        username = request.getParameter(username);
        password1 = request.getParameter(password1);
        password2 = request.getParameter(password2);
        email = request.getParameter(email);

        if (request.getParameter(register) != null) {
            registerActionPerformed(request, response);

        request.setAttribute(messages, messages);

        request.setAttribute(username, username);
        request.setAttribute(email, email);

        if (account != null) {
                    forward(request, response);
        } else {
                    forward(request, response);

    protected void registerActionPerformed(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        if (validate()) {
            model.user.Account existing = model.user.AccountRepository.
            if (existing != null)

            existing = model.user.AccountRepository.getByEmail(email);
            if (existing != null)

            if (messages.isEmpty()) {
                model.user.RegisterAccountService service = 
                        new model.user.RegisterAccountService();
                account = service.register(username, password1, email);

    protected boolean validate() throws Exception {
        if (username == null || username.trim().length() == 0)

        if (password1 == null || password1.trim().length() == 0)

        if (password1 != null)
            if (!password1.equals(password2))

        if (email == null || email.trim().length() == 0)

        return messages.isEmpty();

    private model.user.Account account;

    private java.util.ArrayList messages;

    private String username;
    private String password1, password2;
    private String email;

c.handleRequest(request, response);

Next, create the default.jsp and success.jsp pages. For example, default.jsp looks like this.

< %@page contentType=text/html%>
< %@page pageEncoding=UTF-8%>
< %@taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%> 
< %@taglib uri=http://java.sun.com/jsp/jstl/fmt prefix=fmt%> 

< !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN

        <h1><fmt:message key=title.register_account /></h1>

I am not sure how viable this is as as way to speed up web application development, but the following benefits are already obvious. – index.jsp serves as the controller.

  • There is no need to re-compile after adding controllers since JSP pages are compiled automatically, thus speeding up code-test cycle.
  • There is no need for servlet mappings. The folder structure and the designation of index.jsp as a welcome file take care of that.
  • URLs are clean and clear.

There are certainly weaknesses in using this scheme, but as I wrote at the beginning, I am still experimenting with this.

Join the Conversation

1 Comment

  1. You have re-created the idea of a single-page dispatcher as used in many, many ColdFusion programs going back to at least 1999. The idea is not bad, rather it is very workable. However, it does violate the accepted current-day norm of separating the User Interface code with the Business Logic.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.