0

Almost everywhere you can read that you should use EL instead of scriptles in JSP. And I agree that you should not use any JAVA code in JSP except for calling getter methods on model objects. This is quite obvious for me and I don't need any clarification here. But there is this one thing that I really cannot understand is the lack of support for EL in Eclipse IDE.

Let's start with comparing some code. First EL example:

${user.name}
${user.surname}

And now some scriptlet/JSP expression example:

<jsp:useBean id="user" class="com.mycompany.User">
    <%= user.getName() %>
    <%= user.getSurname() %>
</jsp:useBean>

It clearly visible that EL is more elegant. However, Eclipse doesn't provide any autocompletion nor refactoring support for EL. And for JSP expressions it works like a charm. If you delete getName() method from Java class, JSP validator will show you an error during compilation. If you rename the method, all the connected JSP files will update automatically. Moreover, even when you are looking for references to method getName(), Eclipse will show you all calls across Java/JSP files (provided you used JSP expressions/scriptlets). And none of these features are available for EL.

Some of the programmers might say that they don't really care and EL syntax is much better for them, period. But I work with a quite complex legacy code (that I didn't write) in my current project and believe me that the refactoring of EL/model objects is a nightmare. There are a lot of DTO objects extending and composing other DTO objects that are being used on multiple JSP views. Every change is difficult, because I cannot tell at compile time if I didn't break anything.

Based on that experience I just cannot understand how anybody could suggest using EL instead of JSP expressions. Please tell me that I'm missing something and that either using JSP expressions is correct (in my example) or that there is a way to enable all the advantages of using type safety with EL.

EDIT:
I know that IntelliJ has better support for JSP, but I would like to focus on Eclipse first. I hope that there are still some people working on JSP projects on Eclipse/Spring Tool Suite.

1

Every IDE has its good points and bad points. A primary good point of Eclipse is its price. There are other IDEs (such as Intellij) that have comprehensive jsp aware features. Though there are ways around this by adding additional type information to the beans that should help in all IDEs.

That said, the real question here:

Based on that experience I just cannot understand how anybody could suggest using EL instead of JSP expressions. Please tell me that I'm missing something and that either using JSP expressions is correct (in my example) or that there is a way to enable all the advantages of using type safety with EL.

Here's the real answer... you've got some Java code mixed in there.

  • How do you test the code you wroteit? You can't unit test a scriptlet or a jsp page. You can unit test a taglib.
  • You can't reuse scriptlets, so you are violating DRY
  • You can't debug an exception part way through (you get a blank page)
    • As the JSP is compiled at runtime, it is possible/likely to get a compile time exception with a scriptlets that is exceedingly difficult to track down... at run time.
  • You can't make use of inheritance for them (they can't be extended or encapsulated)
  • You do get name space collisions, and ${diety} help you if you <%@ include ... %> something that has a collision between names in different files
  • You are mixing view logic and business logic together
  • Automatic refactoring tools do not recognize them

While they are not inherently evil and there are sometimes necessary tasks to do with them, they are often a significant and ugly thing that causes problems down the road.

Related reading:

  • 1
    Thank you for quick answer! However, I'm afraid I wasn't clear enough. First of all I have to stick with Eclipse (in my company all people are using it). Secondly, I don't want to use scriptlets for any business logic. I'm just comparing calling getter methods from JSP expressions vs EL. And based on that assumption, all these arguments against JSP scriptlets don't apply here. Because I can say all the same things about calling getter methods via EL. Even worse, because Eclipse supports automatic refactoring of JSP expressions, but not of EL. – Kacper86 May 24 '14 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.