Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL vs JSP Scriptlets

Tags:

java

jsp

jstl

I want someone to explain some points in BlausC's amazing answer in this question.

He said that scriptlets had some disadvantages, which are:

  1. Reusability: you can't reuse scriptlets. My question : how could I reuse JSTL code?

  2. Replaceability: you can't make scriptlets abstract. What does abstract mean and how could JST become abstract?

  3. OO: you can't make use of inheritance/composition. How could I use OO paradigms in JSTL?

  4. Debugging: if a scriptlet throws an exception halfway, all you get is a blank page.

  5. Testability: scriptlets can't be unit tested. What does that mean, and how can JSTL be unit tested?

  6. Maintainability: per saldo, more time is needed to maintain mingled/cluttered/duplicated code logic. What does this mean?

The last thing is what he quoted form Oracle's recommendation:

JSP scriptlets should not be used for writing business logic.

In the MVC pattern, I use scriptlets only in the presentation layer. What does he mean here?

like image 303
palAlaa Avatar asked Dec 26 '10 21:12

palAlaa


People also ask

What is difference between JSTL and JSP?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).

What are the advantages of JSTL over JSP?

Advantage of JSTL Fast Development JSTL provides many tags that simplify the JSP. Code Reusability We can use the JSTL tags on various pages. No need to use scriptlet tag It avoids the use of scriptlet tag.

What is wrong in using JSP scriptlet tags?

Its not a clean design to mingle code with view logic. This is why JSP is not ideal solution. You should use templates like Velocity/Freemarker instead which does not allow mixing java code at all.


1 Answers

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. I.e. 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.

like image 185
BalusC Avatar answered Sep 22 '22 17:09

BalusC