Making web development a bit more simple

3. January, 2010

When someone starts to make a path, most people tend to follow the trail. So when Sun introduced the Servlet API, most people were uneasy about the design problems:

  • Servlets are singletons (which are evil; this is due to performance problems in Java 1.2 which have been fixed about ten years ago).
  • The resulting code smells (feature envy since you call many methods of HttpServletRequest/Response, long method since you can’t call subdivide the doGet() without passing at least two parameters to each new method, duplicated code since you have to walk instance trees to get at a lot of information, …)

Whenever I see Java servlet code, I always wonder why people don’t solve this. It’s so simple:

public abstract class ServletRequestHandler {
    protected HttpServlet servlet;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected String contentType = "text/html";

    public void init(
        HttpServlet servlet, 
        HttpServletRequest request, 
        HttpServletResponse response
    ) {
        this.servlet = servlet;
        this.request = request;
        this.response = response;
    }

    public abstract void process();

    public PrintWriter out() {
        if( null == out ) {
            response.setContentType( contentType );
            
            try {
                out = response.getWriter();
            } catch( IOException e ) {
                throw new RuntimeException( 
                    "Can't get writer", e 
                );
            }
        }
        return out;
    }
    
    public ServletRequestHandler html( String html ) {
        out().print( html );
        return this;
    }
    
    public ServletRequestHandler attribute( String value ) {
        out().print( escape( value ) );
        return this;
    }
    
    public ServletRequestHandler attribute(
        String name,
        String value
    ) {
        if( StringUtils.isBlank( value ) ) {
            return this;
        }
        
        PrintWriter out = out();
        out.print( ' ' );
        out.print( name );
        out.print( "=\"" );
        out.print( escape( value ) );
        out.print( "\"" );
        return this;
    }
    
    public ServletRequestHandler text( String value ) {
        out().print( escape( value ) );
        return this;
    }
    
    // Add more methods to support your favorite
    // web design patterns
}

Now, all your servlets look the same: They accept the request, create a new instance of the right implementation of ServletRequestHandler, and call init() plus process(). With a bit of glue code, you can even use Spring to manage all your servlets for you. No more dabbling with web.xml!

And the best part: This pattern allows you to test the servlet code from a unit test without any container. Just use Spring or Mockrunner to get mock-ups for the J2EE API classes.


Nation and the 6th part of the Hitchhiker

1. January, 2010

Just finished reading two books: “Nation” (Terry Pratchett) and “And Another Thing …” by Eoin Colfer.

When I browse through my favorite book store here in Zurich, I’m always looking for something new by Terry Pratchett. I’m a huge fan of his Diskworld series and always torn when there still isn’t another volume out. On one hand, I really miss his witty way to look at the world, on the other, a good thing takes time. So this time, I ambled into the other works of Pratchett but after the the carpet people and “Johnny and the bomb”, I wasn’t too thrilled. But I couldn’t walk away from “Just possibly the best book Pratchett has ever written” (Fantasy and Science Fiction).

And it is. It’s a hugely different setting than Diskworld but as witty and smart as you’d expect. It’s the story of a boy who sets out to become a man and becomes so much more. It’s about standing up against peril, evil and bullies. If you like Diskworld, you must read this, too.

Eoin Colfer was a similar issue: Part 6 of the THHGTTG? You’ve got to be kidding! I loved the stories around Artemis Fowl but The Hitchhiker? Is Eoin out of his mind? Luckily, he asked himself the same questions.

The net result: Definitely not a book by Douglas Adams but also definitely a book from the Hitchhiker series. Ever wondered where the animals come from that want to be eaten and can argue in their favor? There must be herds, right? There are. When Thor (the Norse god) needs some aiming practice, they “provide moo-ving targets”. Just like Adams, Eoin (pronounced Owen) likes to take things to the tip and I mean the utmost protruding electron. It’s a book about a world where all your wishes were granted. And you know the old saying. A fun read and at least one good laugh on every page. To put it another way: The worst thing about the book is its title.

If you’re still worrying whether you should dare to complete the trilogy with part 6, stop and buy.

Recommendation: Buy. Both. Now.