The Source for Java Technology Collaboration
User: Password:



Simon Brown's Blog

November 2005 Archives


Comparing webapp frameworks : Model 1 with scriptlets

Posted by simongbrown on November 23, 2005 at 11:42 AM | Permalink | Comments (1)

Before we dive into the frameworks, I want to drop back to basics to give some context behind why the frameworks exist and what benefits they provide. For this reason, let's look at a naive model 1 implementation of the sample application. If you're already familiar with the whole model 1 vs. model 2 thing, you might want to skip reading this particular entry.

Overview
An in-depth explanation of model 1 vs. model 2 can be found in Designing Web Applications and Servlet Patterns, but essentially a model 1 web application architecture is where all of your logic is wrapped up inside the pages of your application. By logic, I'm referring to things like business logic, presentation logic and page flow. Let's take a look at some examples of this by seeing how a model 1 version of the sample application might be built.

Home page
From a JSP perspective, a model 1 web application is very easy to build because you just start coding away in your JSP pages. Here's what the home page looks like when implemented using a model 1 architecture.

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="domain.*,
                 java.util.Iterator,
                 java.text.DateFormat" %>
<%
  BlogService blogService = new BlogService();
  Blog blog = blogService.getBlog();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <title><%= blog.getName() %></title>
    <link rel="stylesheet" href="screen.css" type="text/css" />
  </head>

  <body>
    <div id="container">
      <h1><%= blog.getName() %></h1>
      <h2><%= blog.getDescription() %></h2>

      <%
        Iterator it = blog.getBlogEntries().iterator();
        while (it.hasNext()) {
          BlogEntry blogEntry = (BlogEntry)it.next();
      %>
        <div class="blogEntry">
          <h3><%= blogEntry.getTitle() %></h3>

          <%
            if (blogEntry.getExcerpt() != null) {
          %>
            <%= blogEntry.getExcerpt() %>
            <p>
            <a href="viewBlogEntry.jsp?id=<%= blogEntry.getId()%>">Read more</a>
            </p>
          <%
            } else {
          %>
            <%= blogEntry.getBody() %>
          <%
            }
          %>

          <p>
          <%
            DateFormat dateFormat = DateFormat.getDateTimeInstance(
              DateFormat.LONG, DateFormat.LONG, blog.getLocale());
            dateFormat.setTimeZone(blog.getTimeZone());
          %>
          Posted on <%= dateFormat.format(blogEntry.getDate()) %>
          </p>
        </div>
      <%
        }
      %>
    </div>
  </body>

</html>

I'm not going to explain each and every line, but the basic structure breaks down as follows.

  1. Define page characteristics and import Java classes.
  2. Locate the Blog instance to work with, via the BlogService.
  3. Write out the page header, title, etc.
  4. For each blog entry, write out the appropriate properties.
  5. Close all the tags.

For the "read more" link, this is simply implemented as a call to the blog entry detail JSP page, passing the blog entry ID as a parameter. Notice how this is conditionally included, depending on whether the excerpt is null - it's a good example of the simple presentation logic that you'll find in many web applications. The date formatting code is another example.

Blog entry detail page
The blog entry detail page is very similar.

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="domain.*,
                 java.text.DateFormat" %>
<%
  BlogService blogService = new BlogService();
  Blog blog = blogService.getBlog();
  BlogEntry blogEntry = blog.getBlogEntry(request.getParameter("id"));
  if (blogEntry == null) {
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return;
  }
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <title><%= blogEntry.getTitle() %> : <%= blog.getName() %></title>
    <link rel="stylesheet" href="screen.css" type="text/css" />
  </head>

  <body>
    <div id="container">
      <h1><%= blog.getName() %></h1>
      <h2><%= blog.getDescription() %></h2>

      <div class="blogEntry">
        <h3><%= blogEntry.getTitle() %></h3>

        <%= blogEntry.getBody() %>

        <p>
        <%
          DateFormat dateFormat = DateFormat.getDateTimeInstance(
            DateFormat.LONG, DateFormat.LONG, blog.getLocale());
          dateFormat.setTimeZone(blog.getTimeZone());
        %>
        Posted on <%= dateFormat.format(blogEntry.getDate()) %>
        </p>
      </div>
    </div>
  </body>

</html>

The scriptlet at the top of the page is probably the most interesting because that piece of code is responsible for locating the requested blog entry or sending the user off to an error page, which is simply configured through the web.xml file with the following XML fragment.

<error-page>
  <error-code>404</error-code>
  <location>/404.jsp</location>
</error-page>

Summary
Both pages contain a short piece of Java code at the top of the page to "set the scene". This type of logic is typically what you find scattered throughout the pages of a model 1 webapp, the downsides of which include reduced maintainability, reusability and testability. Both additionally contain several other short pieces of Java code to handle some presentation logic such as iteration, conditional processing and date formatting. As we'll see next, the JSP Standard Tag Library (JSTL) can help to solve at least some of these problems.



Comparing webapp frameworks : Domain model

Posted by simongbrown on November 09, 2005 at 04:21 AM | Permalink | Comments (1)

Before we kick off our look at webapp frameworks, let's establish the domain model we're working with. It's pretty simple - blogs and blog entries, residing in a package called domain.

Domain model : BlogService, Blog and BlogEntry

Because I want to concentrate on the presentation aspects of the frameworks this time around, I'm ignoring the persistence mechanism. Instead, I'm just assuming that data access will be achieved via a "service", regardless of whether it uses direct JDBC, a DAO, a web service, Hibernate, etc.

So that's the model. The question is, will any of the frameworks or view technologies require us to change it?



Comparing webapp frameworks : Requirements

Posted by simongbrown on November 04, 2005 at 05:45 AM | Permalink | Comments (8)

So, to compare webapp frameworks we need an example web application. I've chosen to build a simple blog. Here are the functional requirements.

  1. The home page will display a list of the 3 most recent blog entries, in reverse chronological order. The following information will be displayed for each blog entry.
    • Title
    • If an excerpt is present, the excerpt will be displayed with a "read more" link to the full blog entry.
    • If no excerpt is present, the full body will be displayed.
    • Date/time
  2. When a "read more" link is clicked, the resulting page will display the following information about the selected blog entry.
    • Title
    • Body
    • Date/time

And here are some non-functional requirements, just to make things a little more interesting.

  1. The web application will make use of and validate against the following standards.
    • XHTML 1.0 Transitional
  2. HTTP status codes will be used where appropriate (e.g. 404 for page not found).
  3. UTF-8 will be used as the character encoding to support international character sets.
  4. Dates and times will be formatted appropriately for the locale of the owning blog.
  5. Dates and times will be presented in the time zone of the blog.
  6. A strict MVC architecture will be adopted, with all access to views being made through the controller.
  7. The technology constraints are as follows.
    • Java SE 5.0
    • Servlet 2.4 and JSP 2.0
    • Tomcat 5.5.x

Here are some screenshots to show what this all looks like.

Home page
Figure 1 : Home page.

Blog entry detail page
Figure 2 : Blog entry detail page.

Page not found page
Figure 3 : Page not found page.

As I've said before, I hoping to make this an iterative process and there's plenty of scope for adding some interactivity into the example application. Next up is a look at the domain model.



Comparing webapp frameworks : Why?

Posted by simongbrown on November 03, 2005 at 11:59 AM | Permalink | Comments (11)

After posting Comparing webapp frameworks : Introduction yesterday, I got lots of feedback - some good, some bad. So, why am I doing this?

First off, here are some of the negative comments.

  • Imho this is a complete waste of time and it will be another biased comparison without any real use whatshowever. Please spend your time on something useful and don't add another confusing hyped comparison to the mix.
  • A propos, the easy 80% is rarely the problem and often very repetitive. Most of it is even getting totally automated with CRUD frameworks. The interesting bit is the difficult 20%, this is where the actual difficulties lie and where most time is spent.
  • No one cares about how each framework handles simple things.
  • If I wanted to compare frameworls your way I'd visit every one of the frameworks web sites...
  • Many frameworks sell there idea demonstrating how easy it is to do simple stupid examples.

Fantastic! At least people are talking and I got some response. Great to see that the community is alive. :-)

Choice
When choosing a platform to build a web application, J2EE is still the first choice for many. As we're all well aware, new webapp frameworks are released fairly often and I believe that there's still a lot of room for innovation, particularly when you look "Beyond Java", with stuff like Ruby on Rails. So, from this perspective, one of the reasons that I'm doing this is to provide an "at a glance" view of many frameworks as opposed to going in deep on a few. After all, I want to be in a position to answer questions like, "do you think we should use Struts or something newer like Stripes?". I suspect that most people would comment that both are webapp frameworks, but recommend Struts because it's proven. I could be wrong, but I do think that even an "at a glance" view offers something of value.

Nobody cares about the simple stuff
Taking on the comment about the 80% being easy and repetitive - yes, that's exactly what I was getting at. It is easy, or at least it should be. That's something else that I want to look into. If a particular framework makes the easy stuff hard, I'm not going to recommend it to my clients. Where's the productivity gain in slowing 80% down but making 20% really fast? True, some of the easy stuff can be automated with CRUD style webapp frameworks, but this isn't necessarily the world we live in. Just taking an example, one of my current clients is building a web presence over the top of a service oriented architecture, which is deployed on an enterprise service bus. How do those CRUD frameworks help me here?

Consistency and reduced learning curve
Why should I have to compare frameworks by visiting each and every website? They all differ in terms of content, layout, documentation, examples, etc. By implementing even a simple application, I can reduce the learning curve for those people that just want to quickly skim the frameworks and pick up the main points. This is made even easier if the code for the same application is available for multiple frameworks.

The other thing to pick up on here is that some of the framework sites do have incredibly simple examples. They're even simpler than the application I've come up with and that's a problem. Once you stray outside of the simple example you have to start digging around the docs. If I can at least go slightly deeper than the very simple examples, again, I think this adds value.

The evolution of web MVC
Another reason for doing this is to see how the web MVC pattern has evolved over the past few years. I don't want to jump ahead too much, but there are some very interesting and unique ways that this seemingly simple pattern has been implemented. I want to compare and contrast this too. Coupled with the view technologies, I want to see how these various choices affect what is ultimately the core architecture of your web application.

Just to clarify, my sample application is read-only from the perspective of the user. The content is dynamic, but the user can't update it. As I said, I am hoping to make this an iterative process and revisit it to add further levels of detail. I've got to start somewhere though. ;-)



Comparing webapp frameworks : Introduction

Posted by simongbrown on November 02, 2005 at 12:21 PM | Permalink | Comments (9)

Struts, WebWork, Stripes, Spring MVC, Wicket, Tapestry, JSF, etc, or even rolling your own. With so many J2EE web application frameworks to choose from, how do you decide which one to use? Several articles (e.g. JavaServer Faces vs Tapestry) and presentations (e.g. Comparing Web Frameworks) already exist, but they generally concentrate on a small subset of the available frameworks.

Over the next few weeks, I'm going to implement a small web application using as many J2EE web application frameworks that I can get my hands on. I'll be using the resulting code to compare and contrast what the frameworks provide and how they work.

Clearly this is a massive task so, to reduce the scope, I'm going to focus on what it takes to build a read only web application. If I were to hazard a guess, I'd say that the 80-20 rule applies. 80% of a web application is read only and 20% is interactive (e.g. HTML forms, AJAX, etc). Of course, this is changing with technologies like AJAX, but we're still on the upward curve. Traditionally, that 20% is the most complex and is an area where many web application frameworks claim their unique selling points. For this reason, I may iterate over the evaluation process to take into account how the frameworks help web developers build interactive webapps. For now, I'm going to look at whether the frameworks make doing the 80% easy.

In addition to looking at the webapp frameworks, I'm also going to compare and contrast some of the view technologies that are currently available. Here, I'm talking about JSP (normal and XML formats), Velocity, FreeMarker, etc. I think this adds an interesting twist and your choice of view technology may have an impact on your framework decision.

Stay tuned, it should be an interesting journey!





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds