Skip to main content

Comparing webapp frameworks : Model 1 with JSP XML

Posted by simongbrown on January 12, 2006 at 2:49 AM PST

For completeness, I wanted to show how the JSP pages from the JSTL version could be written using the JSP XML syntax. Unsurprisingly, many people don't even know of its existence and, as I've blogged before, there aren't that many situations where you'd want to use it to write pages by hand. Should you need to, here are a couple of examples of how you would go about it using the XML syntax.

Home page

Here's the home page once again, implemented using the JSTL and written in the JSP XML syntax.

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root xmlns="http://www.w3.org/1999/xhtml"
   xmlns:jsp="http://java.sun.com/JSP/Page"
   xmlns:c="http://java.sun.com/jstl/core_rt"
   xmlns:fmt="http://java.sun.com/jstl/fmt_rt"
   version="2.0">

  <jsp:useBean id="blogService" scope="request" class="domain.BlogService"/>
  <c:set var="blog" value="${blogService.blog}" />

  <jsp:output doctype-root-element="html"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system="http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
  <jsp:directive.page contentType="text/html;charset=UTF-8" />

  <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
      <title>${blog.name}</title>
      <link rel="stylesheet" href="screen.css" type="text/css" />
    </head>

    <body>
      <div id="container">
        <h1>${blog.name}</h1>
        <h2>${blog.description}</h2>

        <c:forEach var="blogEntry" items="${blog.blogEntries}">
          <div class="blogEntry">
            <h3>${blogEntry.title}</h3>

            <c:choose>
              <c:when test="${not empty blogEntry.excerpt}">
                ${blogEntry.excerpt}
                <p>
                <a href="viewBlogEntry.jsp?id=${blogEntry.id}">Read more</a>
                </p>
              </c:when>
              <c:otherwise>
                ${blogEntry.body}
              </c:otherwise>
            </c:choose>

            <p>
            Posted on <fmt:formatDate value="${blogEntry.date}"
                        timeZone="${blog.timeZone}" type="both"
                        dateStyle="long" timeStyle="long" />
            </p>
          </div>
        </c:forEach>
      </div>
    </body>

  </html>

</jsp:root>

In comparison to the previous implementation, the major difference here is that the JSP page must be well formed XML, with everything wrapped up inside a <jsp:root> element. In addition, the taglib directives are replaced by a simple XML namespace, which is really quite neat.

Blog entry detail page

The blog entry detail page is fairly similar in all respects, with the Java code at the top of the page now being wrapped up inside a <jsp:scriptlet> element.

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root xmlns="http://www.w3.org/1999/xhtml"
   xmlns:jsp="http://java.sun.com/JSP/Page"
   xmlns:c="http://java.sun.com/jstl/core_rt"
   xmlns:fmt="http://java.sun.com/jstl/fmt_rt"
   version="2.0">

  <jsp:output doctype-root-element="html"
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system="http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
  <jsp:directive.page contentType="text/html;charset=UTF-8" />
  <jsp:directive.page import="domain.*" />

  <jsp:scriptlet>
    BlogService blogService = new BlogService();
    Blog blog = blogService.getBlog();
    request.setAttribute("blog", blog);

    BlogEntry blogEntry = blog.getBlogEntry(request.getParameter("id"));
    if (blogEntry == null) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    } else {
      request.setAttribute("blogEntry", blogEntry);
    }
  </jsp:scriptlet>

  <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
      <title>${blogEntry.title} : ${blog.name}</title>
      <link rel="stylesheet" href="screen.css" type="text/css" />
    </head>

    <body>
      <div id="container">
        <h1>${blog.name}</h1>
        <h2>${blog.description}</h2>

        <div class="blogEntry">
          <h3>${blogEntry.title}</h3>

          ${blogEntry.body}

          <p>
          Posted on <fmt:formatDate value="${blogEntry.date}"
                      timeZone="${blog.timeZone}" type="both"
                      dateStyle="long" timeStyle="long" />
          </p>
        </div>
      </div>
    </body>

  </html>

</jsp:root>

Summary

All in all, there aren't massive changes between writing a page in the native JSP syntax and writing it in the XML syntax, but care does have to be taken to ensure that your JSP pages are valid XML and easily readable if using the latter. Personally, I've never seen the XML syntax in use on projects that I've been involved with but the choice, as they say, is yours.

Right, let's put model 1 behind us and start comparing some webapp frameworks. :-)

Related Topics >>