jsp: a study in overengineering

Geek

What is it with Java Server Pages that makes them such a pain the arse to use? I have only used JSP v2 and JSTL 1.1, but I hate to think how hard this technology was to use before the current version, with things like the Expression Language and JSP Documents.

There has clearly been a lot of thought put into JSP and related technologies and it is possible to do a lot with it. But why on earth are the simple things so hard to do? Take tag collaboration for example. Why is it impossible to share attributes between nested tag files without resorting to using embedded expressions or JSTL set tags? Why can't I get to a parent's tags attributes using the EL, hell, why can't I get to the parent tag or even this tag with the EL?

On scoping, with four different scopes (page, request, session, application), the spec seems to have all bases covered, but is it worthwhile having variable names resolved against all four? Hands up who ever needed to fall back on variables declared in the application scope, actually, who has even used application scope? I thought so. Worse, the page scope in a tag file is different to the page page scope for the calling JSP? Why?! How do you get back to the page scope for the calling page? Using annoying directives and jumping through hoops? Yay!

The effective nesting of the four different scopes is a nifty trick, but for little tangible benefit and greater chance for confusion. I think a better solution would have been to keep variable resolution seperate for the four different scopes. Keep the implicit objects for each (you need them to access the variables they contain) but just don't nest them.

To make tag collaboration easier, make the page scope global for the current JSP page and all included tags. Adding a variable to the page scope in one tag should make the variable visible to the page and all remaining tags. Then, include a "this" scope for tags to allow you to get to the tag, its attributes and its parents and siblings. This way we can get rid of variable (or is that "attribute") synchronisation, it is an annoying kludge.

There is a tangle mass of terminology surrounding JSPs that makes it much harder to work with, especially when using tags. When you're talking about attributes, are you referring to a variable in one of the four scopes (so called because that is what the Servlet API calls them, which is a pretty lame excuse) or an XML attribute with an equivalent accessor on the tag's class (which is somehow magically copied to the, err... page scope I think, when the tag is invoked). Why are these variables called attributes, when they aren't? Then think about the variable and attribute directives, and what they actually do. My god... it's full of stars...

Ah yes, the "variable" directive. What an obfuscated mess. Who decided it would be a good idea to call the type attribute (heh, there is that word again) "variable-class" rather than just "type", as it is in the attribute directive (and there it is again). Why "name-given" rather than just "name"? What is "alias" an alias of? Why does the one directive perform two very different tasks? It exists purely as an attempt to unbreak variable scoping in tags, why not just ditch it and fix the scope?

Most of this criticism is centred around tags, specifically tag files (as opposed to tags implemented as classes) but I believe that is where the main payoff for JSPs can be found: the ability to quickly refactor common markup out and easily create reusable behavioural and presentational tags. The introduction of tag files in version 2 made this actually possible, so who knows, maybe version three will make them easy to use. This is one big area where technology like PHP and Rails comprehensibly wipes the floor with the J2EE stack. Stuff like this should be fast and easy to do, simple to understand and not wrought with arcana.

Include here also rants about not being able to call functions in the EL, the oversupply of XML configuration files needed in addition to code, the difficulty debugging errors with the most popular implementation, a scarcity of useful online documentation and so on.

There is a maxim that goes something like "make the simple things easy and the difficult things possible" but this seems to have been lost on the current iteration of the JSP spec. Which is a pity given how widely is is used.

NB: Jason, this isn't related to our conversation, I'm just venting after a day's wrestling with the suckers at work.

Posted Tuesday, September 20, 2005 at 23:20.

TrackBacks

TrackBack URL for this post: http://volition.vee.net/mt/mt-idle-trackback.cgi/511

Comments

That saying comes from Perl, man.

Posted by: Joel on September 21, 2005 12:58 AM

Hey, don't hold that against it (the saying, that is).

Posted by: Mike on September 21, 2005 09:38 AM

Get yourself a Mac and then use WebObjects! :)

Posted by: Shaun on September 21, 2005 10:48 AM

WO? I hear they can't even give it away at the moment... I wonder why that would be?

;)

Posted by: Mike on September 21, 2005 11:33 PM

Man - why on earth would anyone be using jsp? I've had two jobs where I've had to write jsp - never again. Quit the job if it's a legacy thing they're making you work on. If you're doing a new project, use tapestry. Bit of a learning curve but it thrashes jsp in every way possible, and is no less efficient (and will probably end up being far more efficient because of the algorithms you use, etc).

Posted by: Craig Turner on September 26, 2005 09:52 AM

The use of JSP in this project was largely because it is the standard Java (i.e. EE) way of doing it. I am not a big fan of the technology (see extended rant above) but it is vaugely usable. Like most technologies, the amount it sucks depends on how it is wielded.

JSP v2 looks a lot better than earlier versions (although I have never actually used any earlier versions). The improvements to the JSP Document syntax and the EL also makes life a lot easier, but the introduction of tag files are really what have made using JSP tenable.

So which JSP version were these projects you were working on using? Were you using v2, document pages and tag files?

Posted by: Mike on September 26, 2005 11:23 AM

Add a Comment



(Optional)


(Optional)


Preview your comment before submitting.