Re: JSP still relevant
On Thu, 25 Jun 2009, Tim Slattery wrote:
Lew <noone@lewscanon.com> wrote:
SL Da wrote:
I have learnt a bit of JSP.
Before I delve deeper, I just wonder whether JSP is relevant, or will
still be relevant.
Tim Slattery wrote:
Absolutely. Java Server Pages is kind of servlets turned inside out. A
servlet is a Java program that outputs HTML. A JSP page is mostly
HTML, with Java code interspersed. I use the Struts framework which
It is a best practice to have no raw Java scriptlet whatsoever in a JSP.
You use tags instead, sure. Given the JSTL and the many other taglibs
available, you don't need to use actual Java code hardly at all.
I know this is conventional 'best practice' advice, but i think it's
actually really bad advice.
The problem is that just banning scriptlets doesn't ban code from JSPs, it
just means people write the code using tags instead - and tags are a
really, really bad programming language, like something from the 50s with
the added bonus of a massively verbose syntax. So rather than:
<%
boolean onFirstPage = pageIndex == 0;
if (onFirstPage) out.println("You are on page " + (pageIndex + 1));
else out.println("You are on the first page");
%>
(note that i want to leave onFirstPage as a variable, so i can use it
again later)
You have some monstrosity like:
<c:set var="onFirstPage" value="${pageIndex == 0}"/>
<c:choose>
<c:when test="${onFirstPage}">
You are on page <c:out value="${pageIndex + 1}"/>
</c:when>
<c:otherwise>
You are on the first page
</c:otherwise>
</c:choose>
You wouldn't seriously claim that was better, would you? And this is just
a mild example!
I work with a big hairy CMS webapp whose JSPs are almost entirely made of
convoluted logic expressed using tags, far worse than the above. If it was
done with scriptlets, it would be more compact, more readable, much, much
more debuggable, and more maintanable. Oh, and it would be faster.
The problem is not java in JSPs, it's logic in JSPs. If you factored the
above code out into a custom tag, or a bean or something, where it could
be written in java and then cleanly integrated with JSP, i think that
would be ideal. But as long as the advice is "don't use scriptlets",
rather than "don't put logic in JSPs", that's not what people are going to
do.
tom
--
.... the gripping first chapter, which literally grips you because it's
printed on a large clamp.