• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why JSTL than scriptlets?

 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ranchers,
Please anyone....
Why JSTL than scriptlets?

Vilas
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scriptlets are hard to read and hard to maintain. JSP was improved throughout its history, so that even people who don't know Java (well, a limited knowledge of it) could make presentation pages.
 
Kunal Jag
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL offers a superset of JSP functionalities. JSTL can help to avoid using scripting elements in JSP pages. As a matter of practice, a JSP page should be a view page, it should not have Java code embedded in scriptlets. This aids in separation of concerns. A JSP page can use JSTL elements for common tasks like conditional processing, database access, internationalization and more.
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.

Vilas Lawande
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL has its natural place alongside JSP standard actions and EL. While something which can be achieved using these approaches is preferred, I think it's important to realise scriptlets aren't dead. For example, suppose I have a method which checks the expiry date on a product of the form:and some implementing subclass. Now I need to display this promotional item if and only if isExpired() returns false for the current date. Then I can't do this using only JSTL and EL - because EL doesn't support passing arguments to Java methods (recall, EL functions are a totally different paradigm). So in this case (assuming the object 'promo' is a subclass of Promotion) it becomes necessary to use a scriptlet like:Not only does this do what we now require, but in fact it's more efficient too. It uses native instructions from the Java bytecode (method invocations and if test) and in a compact manner. In comparison, using some <c:if test="...">...</c:if> action here would result in a complicated instantiation of the action object and calls to various initialisation and lifecycle methods. It's also generally more efficient using local scripting variables than scoped attributes, since we don't have to do method invocations on each lookup of the variable (as with EL) but instead we just get it's reference directly from the stack.

Although my example could be re-written to get over the problem I illustrate, there are cases in which it cannot or which become unwieldy (and highly inefficient in terms of CPU and memory) to do so - this includes the very inconvenient and unobvious case of moving vast amounts of code into a Front Controller servlet and passing the results via a request-scoped attribute to the JSP so it can be evaluated strictly via JSTL/EL. A common compromise I use is to do all the hard stuff (e.g. filtering items from a long list) in a scriptlet right at the beginning of the JSP, so after that my page is all template text, actions and EL.

Until future EL provides support for Java methods (so EL can be used with general methods other than getters), sometimes scriptlets really are the only way to work. While scriptless pages are the way I write my JSPs wherever possible, sometimes that just isn't a sensible option.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic