3

I've heard a billion times about how horrible it is to use Scriptlets (those php-like <% %> things) in JSP.

  • It has been proven that Scriptlets break the code-design patterns, and usually, the MVC Pattern
  • Many J2EE frameworks (such as JSF 2.0+) have already disabled the usage of scriptlets.

Since scriptlets are not required for any uses, and can be replaced with JSTL, Other taglibs, and EL (Expression Language), why do they still exist? How comes Oracle haven't removed those scriptlets out of the JSP technology yet?

jmj
  • 237,923
  • 42
  • 401
  • 438
Victor2748
  • 4,149
  • 13
  • 52
  • 89
  • 3
    The main reason they haven't been removed is backwards compatibility, but as you said, they are now deprecated. – morgano Jan 07 '15 at 00:27
  • I love scriptlets. I use them all the time. They are very convenient. – rickz Jan 07 '15 at 03:20
  • @rickz Why do you prefer to use scriptlets over JSP taglibs and expression language? – Victor2748 Jan 07 '15 at 03:56
  • 2
    I use them all. But, but you can't beat scriptlets for doing something quick and dirty. I throw some java into a scriptlet and call it with my browser. I make some changes to my code and hit the refresh button. I do that over and over until I am happy with the result. – rickz Jan 07 '15 at 04:13
  • You can't just remove things from products. Nothing has been removed from Java since about 1.1 and the AWT event model change. If then. – user207421 Jan 07 '15 at 06:01

2 Answers2

5

This is three years old question but I’d like to put my personal view on the matter. I use JSPs with scriptlets for over 15 years in big project (CRM system) and here are my experiences:

  • Scriptlets break MVC pattern only if you let them. I don’t see why should be <c:out ...${product.priceVat}> somehow better then <%= product.getPriceVat() %>. Many times it’s actually worse. It’s more verbose and you may loose type information. Is property priceVat BigDecimal or is it formatted string with currency? You somtimes don't know in case of EL (depends on IDE used).
  • Big help for our project are automated refactorings. If one developer renames getPriceVat method to getPriceTax it have to be reflected in all code base. IntelliJ IDEA is great in handling scriptlets. Not sure if it automatically renames ${product.priceVat} to ${product.priceTax} (EDIT: it does, cool). Netbeans or Eclipse don't handle JSP sefactorings so well.
  • Scriptlets perform better than EL/JSTL combo. They have to if you look under the hood. The latter produces bigger code (Apache Jasper) and we run in 64kB code limit for Java method few times and were literally forced to replace some tags with scriptlets.
  • Prototyping. We often prototype JSPs using scriptlets (type code - refresh page - type code - refresh...). Later we enhance our taglibs and clean the page up.
  • Hot fixes. We prevented server restart a few times by directly changing one JSP scriptlet on production server. Impossible if you need to change compiled code inside of a class and need to rebuild the whole WAR file. I know it's nasty.
  • Indentation of code is nightmare with scriptlets. That’s clear disadvantage of using scriptlets.
  • I hope that support for scriptlets will never be deprecated. Ability to disable scriptlets in web.xml should be good enough for clean code purists.
bizwiz
  • 329
  • 3
  • 5
2

They still exist for backward compatibility.

Slash
  • 496
  • 2
  • 8