- The use of scriptlets (those <% %> things) in JSP is indeed highly discouraged since the birth of taglibs (like JSTL) and EL (Expression Language, those ${} things) over a decade ago.
The major disadvantages of scriptlets are:
- Reusability: we can’t reuse scriptlets.
- Replaceability: we can’t make scriptlets abstract.
- OO-ability: we can’t make use of inheritance/composition.
- Debuggability: if scriptlet throws an exception halfway, we get a blank page.
- Testability: scriptlets are not unit-testable.
- Maintainability: per saldo more time is needed to maintain mingled/cluttered/duplicated code logic.
- Oracle itself also recommends in the JSP coding conventions to avoid use of scriptlets whenever the same functionality is possible by (tag) classes.
- From JSP 1.2 Specification, it is highly recommended that the JSP Standard Tag Library (JSTL) be used in your web application to help reduce the need for JSP scriptlets in your pages. Pages that use JSTL are, in general, easier to read and maintain.
- Avoid JSP scriptlets whenever tag libraries provide equivalent functionality. This makes pages easier to read and maintain, helps to separate business logic from presentation logic, and will make your pages easier to evolve into JSP 2.0-style pages (JSP 2.0 Specification supports but deemphasizes the use of scriptlets).
- the model-view-controller (MVC) design pattern to reduce coupling between the presentation tier from the business logic, JSP scriptlets should not be used for writing business logic. Rather, JSP scriptlets are used if necessary to transform data (also called “value objects”) returned from processing the client’s requests into a proper client-ready format. Even then, this would be better done with a front controller servlet or a custom tag.
- How to replace scriptlets entirely depends on the sole purpose of the code/logic. More than often this code is to be placed in a fullworthy Java class:
- If you want to invoke the same Java code on every request, less-or-more regardless of the requested page, e.g. checking if an user is logged in, then implement a filter and write code accordingly in doFilter() method.
E.g.:
- When mapped on an appropriate <url-pattern> covering the JSP pages of interest, then we don’t need to copypaste the same piece of code over all JSP pages.
- If you want to invoke some Java code to preprocess a request, e.g. preloading some list from a database to display in some table, if necessary based on some query parameters, then implement a servlet and write code accordingly in doGet() method.
E.g.:
[ad type=”banner”]- This way dealing with exceptions is easier.
- The DB is not accessed in the midst of JSP rendering, but far before the JSP is been displayed.
- There is a possibility to change the response whenever the DB access throws an exception.
- In the above example, the default error 500 page will be displayed which you can anyway customize by an <error-page> in web.xml.
- If you want to invoke some Java code to preprocess a request, e.g. preloading some list from a database to display in some table, if necessary based on some query parameters, then implement a servlet and write code accordingly in doGet() method.
- This way dealing with exceptions is easier.
- The DB is not accessed in the midst of JSP rendering, but far before the JSP is been displayed.
- There is a possibility to change the response whenever the DB access throws an exception.
- In the above example, the default error 500 page will be displayed which you can anyway customize by an <error-page> in web.xml.
- If you want to invoke some Java code to postprocess a request, e.g. processing a form submit, then implement a servlet and write code accordingly in doPost() method.
E.g.:
[ad type=”banner”]- This way dealing with different result page destinations is easier: redisplaying the form with validation errors in case of an error (in this particular example you can redisplay it using ${message} in EL), or just taking to the desired target page in case of success.
- If you want to invoke some Java code to control the execution plan and/or the destination of the request and the response, then implement a servlet according the MVC’s Front Controller Pattern.
E.g.:
Or just adopt a MVC framework like JSF, Spring MVC, Wicket, etc so that you end up with just a JSP/Facelets page and a Javabean class without the need for a custom servlet.
- If you want to invoke some Java code to control the flow inside a JSP page, then you need to grab an (existing) flow control taglib like JSTL core.
E.g. displaying List<Product> in a table:
With XML-style tags which fits nicely among all that HTML, the code is better readable than a bunch of scriptlets with various opening and closing braces. An easy aid is to configure our web application to throw an exception whenever scriptlets are still been used by adding the following piece
web.xml:
- In Facelets, the successor of JSP, which is part of the Java EE provided MVC framework JSF, it is already not possible to use scriptlets. This way you’re automatically forced to do things “the right way”.
- If you want to invoke some Java code to access and display “backend” data inside a JSP page, then you need to use EL (Expression Language), those ${} things.
E.g. redisplaying submitted input values:
[ad type=”banner”]- If you want to invoke some utility Java code directly in the JSP page (typically public static methods), then you need to define them as EL functions. There’s a standard functions taglib in JSTL, but you can also easily create functions yourself.
- An example how JSTL fn:escapeXml is useful to prevent XSS attacks.
- Note that the XSS sensitivity is in no way specifically related to Java/JSP/JSTL/EL/whatever, this problem needs to be taken into account in every webapplication we develop.
- The problem of scriptlets is that it provides no way of builtin preventions, at least not using the standard Java API.
- JSP’s successor Facelets has already implicit HTML escaping, so don’t consider XSS holes in Facelets.
JSTL offers tags for conditionals, loops, sets, gets, etc. For example:
- JSTL works with request attributes – they are most often set in the request by a Servlet, which forwards to the JSP.
great effort
gud one
Good and Useful