Disadvantages of using scriptlet?

This question already has an answer here:

  • How to avoid Java code in JSP files? 28 answers

  • There are several articles on the web comparing scrptlets and JSTL (I guess you've googled for some first) and most of them will tell you the same

  • JSTL are easier to test, maintain and read
  • JSTL can be reused
  • JSTL can separate business logic from presentation
  • JSTL can just fail without breaking the whole page
  • On the other side

  • JSTL is harder to develop than scriptlets (at least there are different learning curves)
  • To fix a scriptlet in production, it's just a matter of changing the JSP (sometimes the error is more evident)

  • You seem to concentrate on only the presentation and flow-control part of the scriptlets as in using if, for and switch statements and out.print() things.

  • You seem to compare scriptlets 1:1 with JSTL. This is wrong. I was not talking about the flow control part only (which is indeed to be replaced by JSTL), but about writing raw Java code in JSP files in general.

  • Ie gathering request parameters, validating and converting values, interacting with database and other Java classes/methods, etc. All things you normally (indirectly) do in a Servlet or Filter.

  • You should not have scriptlet code in JSPs. I'd recommend 100% JSTL and zero scriplet code.

  • JSPs should be purely presentation. That's the hidden benefit of writing JSPs using only JSTL, because they get all their dynamic data elsewhere. Let the service layer have the business logic and determine what data the JSP needs.

  • This answers your unit testing question, too. You should not have to unit test JSPs; those would be Selenium-like UI tests. If the logic is in the service tier, it's obvious how you test it.

  • JSPs should not be inherited. You can certainly compose them together using something like SiteMesh, but inheritance has no part in your JSPs. Once they inherit from Servlet, the chain should be ended.

  • Besides, it's a false alternative. Neither one should require reuse, inheritance, or unit testing. But that doesn't mean there isn't a clear winner: it's JSTL. No one should be using scriptlets in JSPs, except for very rare one-liners. Scriptlets are begging for trouble.

  • These days I prefer Velocity as my web UI template solution for Java, much more than JSPs. Just my opinion.

  • 链接地址: http://www.djcxy.com/p/76594.html

    上一篇: 如何保持逻辑脱离JSP?

    下一篇: 使用scriptlet的缺点?