JSP 2.0 Thoughts: What a mess

At this point I'm thinking that the evolution of JSP isn't much of an evolution at all, but just complete chaos. Think about it, right now if you want to create a JSP page, you have all these things you can put on your pages: Scriptlets, directives, custom tags, bean tags, JSTL tags, .tag files, Expression Language syntax (EL), EL Functions and XPath queries. I just took a step back and actually LOOKED at the JSP page I was creating and was completely mind-blown.

Here's an example:

<% out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); %>
<%@ page contentType="text/xml;charset=UTF-8"%>
<%@ include file="/include/base.jspf" %>
<%@ taglib prefix="oscache" uri="oscache" %>
<%@ taglib prefix="u" uri="utilities" %>
<%-- main page --%>
<jsp:useBean id="now" class="java.util.Date" />
<c:set var="title"><fmt:message key="Main"/> - <fmt:formatDate value="${now}" dateStyle="full" /></c:set>
<c:import url="/include/header.jsp">
        <c:param name="title">${fn:escapeXml(title)}</c:param>
<x:parse var="mainXML"><c:import url="/xml/index.xml"></x:parse>
<x:forEach select="$mainXML/document/entry">
        <fmt:parseDate pattern="yyyy-MM-dd HH:mm:ss" var="created"><x:out select="created"/></fmt:parseDate>
         <div class="post">
                <h2><x:out select="title" escapeXml="false"/></h2>
                <h3><c:catch><fmt:formatDate value="${created}" pattern="EEEE, MMMM dd, yyyy  h:mm a"/></c:catch></h3>
                <u:images><x:out select="id"/></u:images>
                <x:out select="content" escapeXml="false"/>
                <p class="postlinks">
                        <a href="<x:out select="id"/>.html"><fmt:message key="Permalink"/></a> |
                        <a href="<x:out select="id"/>.html#comments"><fmt:message key="Permalink"/> [<x:out select="comments"/>]</a>
         <p />
<c:import url="/include/footer.jsp"/>

Really... What the hell am I doing!?! I've just slowly piled on more syntactic sugar until I've made the pages completely fucking insane. Now granted, this page is doing *a lot* in a pretty compact space. It's adding header and footer templates, importing all the libraries I need, setting the page type and XML header, incorporating an internationalization bundle, importing a generated xml file (my MVC) which I then parse by using XPath syntax (very cool), uses a custom tag to look for attached images and caches all of this so that there's no major performance penalty.

It's very functional, but I'm starting to go insane looking at it. My coworker Todd looked over my shoulder the other day and said "that's not Java". And he's right - which is a serious problem. This is just a simple page. I've got form pages which would make you gasp.

Let's take a look at that code again. First, it's not even XML!! That's the worst part about it! I'm going through all this rigmarole, and the result isn't valid XML. If you look at the anchor tag (if you can find it) I've got an embedded x:out tag within the href attribute. That's illegal XML syntax, but because I'm using the x: tags I don't have much choice.

A solution to the XML problem would be to use the native Expression Language syntax in my pages and set a variable from the XML output. I don't actually use the native EL much (who does?) just yet, but to see what it'd be like, I mocked up the title in the above example. It really just show how much stuff you could jam into JSP 2.0 if you wanted to. The EL Functions are only in JSTL 1.1 (which needs a JSP 2.0 container). But regardless, it's not like using EL would clean this page up very much, most of it is just various types of tags.

Despite the one scriptlet comment to show that you could indeed throw in scriptlet code, I've actually restrained from doing any Java on the page because this is baaaaaaad (bleats the Java herd). If you're using JSTL or the EL, it doesn't gain you much anyways, since it's such a pain to pass values back and forth - you need to put any variables you create into the Page or Request object map in order for the tags or EL to see them. But the fact is I could still, in a pinch, revert back to JSP and there have been many times I've almost done it just because of the pain I've had to go through to do something simple with the core JSTL logic tags.

Okay. After doing all this, I'll tell you want is really needed. First, tags and functions should be the same thing. If you have an x:out tag, you should be able to see it as an x:out() function. Secondly, tags and functions should be just another .jsp-like file (like the .tag files are now) which are automatically compiled and available to the .jsp pages. If I want to create a u:validateUser() tag/function, I should be able to create it in a .jspf page and just use it as a tag or within an attribute on the page seamlessly. Finally, another automatically-compiled page should be able to be paired up with jsp pages as "logic" pages, with functions that are called when the pages load per request and when they enter and leave the session.

This all needs to be standardized as well, and honestly, be more compatible with Java syntax. The EL is so odd and un-Java like, and because it's not actually compiled like scriptlets, I really have a worry about whether it's peformant. When Sun jumped to JSP 2.0, they missed a chance to break backwards compatiblity and improve the page language. Honestly, after almost 6 years of doing this stuff we should have a much cleaner way of writing web pages. (And JSF isn't it).


< Previous         Next >