Top 5 reasons that JSP scriptlets suck

Learn the top 5 reasons why scriplets and Java code aren't optimal in JSP pages.

Scriptlets and Java code in JSP pages suck. Hereʼs why!


5. 2001 called and it wants its code back!
It's 2011! And while we're still waiting for the jet packs and flying cars that we expected
to have by now, JSP 2.0, which introduced the EL as an intrinsic part of the JSP
translation engine, was introduced in 2002. That's almost a decade ago, and three
years before Ajax was even called Ajax!


Sure there are probably still some antediluvian legacy applications hanging around that
were originally written using scriptlets. But any code written after 2002 should eschew
discredited scriptlets and adhere to modern, best-practice principles.


After all, you wouldnʼt try to create a modern TV with vacuum tubes, would you?


4. Great taste... Less filling!
While the battle over why one would want to drink light beer1 is long passed, the EL
(Expression Language) is both terse and expressive -- much more so than equivalent
Java expressions.


Expressions such as
${person.address.city


are clearer, terser, and infinitely more readable than the equivalent
<%= request.getAttribute("person").getAddress().getCity() %>.


And the perdurable persistent protest that the EL isn't capable of general method calls
will be remedied with JSP 2.2. And while there are those that think that this
accommodation could be the camel's nose under the tent, it will certainly allow
expanded capabilities within EL expressions.


Tastes great, and less filling. Whatʼs not to like?


3. Recycle! Recycle! Recycle!
Just like that aluminum can that you throw into a landfill instead of a recycle bin, Java
code in a JSP is a dead end.


How do you test it? How do you reuse it? How do you document it?


Do the planet a favor and place your Java code where it belongs: firmly ensconced
inside testable, reusable, documentable Java classes.


2. Like reading a RaNsom note!
Just by their nature as templates for HTML pages, JSP pages are like reading a ransom
note with a nightmare of multiple syntaxes! What with JSP tags, JSP directives, HTML
markup, JavaScript code, and CSS rules, the last thing that JSP pages need is a bunch
of Java code adding to the mayhem.


The JSTL, by using the XML-based syntax of JSP custom tags, introduces no additional
or discordant syntax to the JSP milieu, integrating well with the already-resident
syntaxes.


1. All the cool kids are doing it!
While our mothers warned us against jumping off a cliff just because everyone else was
doing it, when it comes to best practices, following the lead of the experienced industry
developers is a really good idea.


No one wants to get laughed at when someone sees their code. Following modern best
practices makes you look smart, informed, and "with it". Join the "cool kids" and make
sure that your JSP pages look sharp and stylish using the JSTL and EL in lieu of grody,
discredited scriptlets.

Dig Deeper on Front-end, back-end and middle-tier frameworks