Skip to main content

Are JSPs dead?

Posted by zarar on April 18, 2007 at 9:10 AM PDT

That’s supposed to be a rhetorical question, of course they’re dead. They died a long time ago when it dawned on us that they were nothing but untestable, overweight slobs that only ever existed because of ASP. Anybody who ever used JSPs has at some point, sworn by them, marveled at how great they are and felt really, really excited to write actual Java code inside pages. But it was only a matter of time until some of the lesser known facts about JSPs became more and more prevalent to the point of that templating languages were forced into creation to combat the shortcomings of Java Server Pages.

JSPs are the ultimate contradiction to Java reusability, there is simply no mechanism in J2EE which allows even an honest man to reuse a JSP. If you’re thinking of <jsp:include>, you’re missing the larger point of reuse - reuse as in other components of the application or even different applications. Of course being confined to a Servlet container kills off any chance of reuse in the SE world along with making testing so hard that you’re forced to mock an application server just to see what’s rendered. The direct binding to a Servlet is also something that never amused me nor did I find the behind the scenes conversion from JSP to Java to Class file ever a needed exercise. Why bother with an extra compilation stage when you’re already serving your page from a compiled Servlet?

Be advised that if you’re still considering using JSPs as the V part in MVC for even a mid-sized application, you’re making a mistake. This is especially true if you’re using JSPs as purely a view mechanism (no actual code in the pages but just taglibs) since you’re not even taking “advantage” of the ability to write Java code in them. If you are one of those folks who thinks the library wasn’t the ultimate example of bad ideas, you might actually still be using scriptlets in your pages and passing them off as acceptable software. You, you just keep doing what you’re doing, there’s no help for you.

Not too long ago a colleague of mine stumbled upon a major bank website (TD Canada Trust) who had managed to seriously screw up their application server configuration and ended up serving the raw JSP page instead of actually executing the code in it. Needless to say the code looked like crap with request.getParameter() being used more often than white space to compute user states in a banking application. It was one of those “this page can do a lot of stuff so I’ll just write a lot of ifs to figure stuff out” type things. What I’m trying to say is that JSPs promote shoving control logic in your pages no matter who you are simply because it’s so easy to fall into the pitfall that are scriptlets. It’s best to eliminate the temptation and use a dumb templating tool to render your views because let’s face it, they’re views!

What to use if not JSPs? Plenty of choices including Velocity, Jamon, Freemarker, StringTemplate, JByte etc. All good options with Freemarker being my favorite because of it’s lightweight nature, templating prowess and out-of-the-box support for Taglibs. Whether you’re templating a simple email to be sent out or a full-fledged page, the concept remains the same. Add in very easy Struts 2 integration for both Velocity and Freemarker and there’s little reason to rely on JSPs to do anything.

I used Selenium as a functional testing tool and realized that I was falling into the pitfall of checking the content displayed on the page to test whether the correct business logic was being performed rather than whether the correct view was being returned (think about it, there’s a difference). If you use a templating engine other than JSP, you can quite easily test the former without having to resorting to a functional testing tool.

As AJAX is becoming more and more popular, JSPs are getting further phased out. Lot of developers prefer to return chunks of HTML from the server rather than an actual dispatched forward. A lightweight and powerful template language is much more suitable here as your AJAX interface view will be an aggregation of snippets of HTML returned from the server. Doing this using JSPs would add a level of complexity that is both undesired and unneeded.

Having said all that, you can produce the same product with both JSPs and other lightweight template engines, but the latter stay much truer to the MVC philosophy and result in a much cleaner code base.

Related Topics >>