Harald Kirsch

genug Unfug.


The Java-Only Web Application (Part I)


No <Angle Brackets>

As mentioned previously I am not a big fan of JSPs. Or let me rather say that I am not a fan of XML as a programming language's syntax in general. Be it JSP, Spring configuration, Ant or XSL. In part I might add web.xml deployment descriptors. All of these are formal languages which have three things in common:

  1. XML is used as the syntax, but not quite. They all use some kind of expression language in a different syntax. Or, in the case of JSP, it is even a mix of at least three syntaxes: HTML/XML, expression languages, Java scriptlets.
  2. All of them pretend to be purely declarative, but at least three contain constructs for loops nevertheless (not sure about Spring). But even for Spring it is true that it does much more than just declaration and or configuration: it is used for full fledged programming.
  3. All of them are interpreted or compiled too late (JSP) for real type safety.

But the pendulum swings back. After years of XML programming with Spring, some time ago the big news was that Spring does without XML "configuration". And Ant is replaced in some communities with angle bracket-free languages. Replacing the angle brackets by floods of '@'-signs in my opinion is still a stupid idea, because I am convinced it can all be done in pure Java.

Trying to prove the case and as an excercise I tried to write the most often used example web application without using angle brackets: a todo list, including a security model and sharing of lists between users. It works well so far. And the ingredients are as follows.

Servlet Engine

Jetty is used as the servlet engine, in particular because it can be completely set up and configured without a single XML file. Everything goes into a main method that configures everything in simple, easy to understand, direct statements like:
    scContext.addServlet(ItemsAddServlet.class, URL_ITEMSADD);


By following the "GET-AFTER-POST" advice (also known as Post/Redirect/Get) and strictly using POST for all changes to the underlying database, servlets naturally devide themselves into two groups. POST servlets change the database to then redirect to GET servlets to display data. This very much reminds of the beaten MVC "pattern" by mapping M to the database, V to the GET-answering servlets and C to the POST-processing servlets.

Generating the HTML

Since I am not using JSP, how do I generate the HTML then? "The code must be full of terrible println() calls", I hear the complaints. Well, of course not! Just because we do not use JSP there is no reason to go completely braindead when it comes to generating HTML. My code is rather full of things like:

    HtmlPage page = pageTemplate();
    Html renderTodolist() {
      Html div = new Html("div")
      .setAttr("class", "row doneSeparator")
      return div;

The example shows that I create a simple DOM tree. It needs one interface and four classes. I refrained from implementing one class for each HTML element to enforce correct HTML attributes and structure. Given that most editors people use to create HTML do not have that either, it seemed overkill.

The nice thing about this approach is that it allows to invoke all the good habits we acquired for clean code to also use on writing HTML, which is quite difficult if the HTML is written in the language hodgepodge called JSP. Eventually the rather trivial DOM tree is send to the user's browser with

      Writer w = response.getWriter();

In the sequel blog post I will describe how to deal with URL parameters and the database.