Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Advantages of JSTL

Now that you have seen how a block of JSP scriptlet and JSTL look, we will discuss some of the advantages and disadvantages of JSTL. We will begin with the advantages.

Advantage: Automatic JavaBean Introspection Support

If you look at the two previous blocks of code, you may wonder what advantage the JSTL version of the for loop really has over the JSP scriptlet version. Isn't the JSTL version longer and more complex? With very simple examples such as these, there is little advantage to JSTL over JSP scriptlets. But if you consider that JSTL's expression language handles JavaBean introspection automatically—which means you don't need to downcast objects you retrieve as scoped attributes—you will see that JSTL often greatly simplifies expressions. Consider a program in which a user bean is held in the session scope. I now want to retrieve the user's name from this object. Using JSP scriptlets, the code would look something like this:

 <% if ((User)session.getAttribute("user")).getName().equals("jheaton") { %>
  ...
 <% } %>

Because of JSP's automatic support of JavaBean introspection, this block of code becomes much more simple in JSTL.

  <c:if test="${user.name == 'jheaton'}">
  ...
  <% } %>

Advantage: Easier for Humans to Read

The basic syntax of Java comes from C/C++, which has its own complex rules for formatting code. I teach a Java class at a local college and often see beginning Java students who only know HTML. These students often have a difficult time when first learning Java syntax. Just learning when to use a semicolon and where to place the three different types of braces that Java uses are hurdles that HTML programmers learning Java must face.

JSTL does not draw its syntax from C/C++ roots. JTSL is based on XML, which is very similar to HTML. Rather than spending a great deal of time learning where to put semicolons and how to construct code blocks, HTML programmers can fall right into a programming language that is similar to HTML (for example, blocks of code in JSTL are delineated by beginning and ending XML tags).

Advantage: Easier for Computers to Read

JSTL can be easier for humans to read, and it is also much easier for computers to read. Tools such as FrontPage and Dreamweaver are generating more and more HTML code. Although HTML generation tools do a great job of formatting HTML code, they often run into problems when this HTML code is intermixed with JSP scriptlet code. Because JSTL is always expressed in XML-compliant tags, it will be easier for HTML generation tools to parse the JSTL code that is contained within the document.

Advantage: Standardized Support for Formatting and I18N

In proper Web design, JSP pages should be responsible only for the presentation of data. Two very common components to this presentation are the formatting of numbers and strings, and internationalization (I18N) support. Prior to JSTL, there was no official way to support these two features in JSP scriptlet code. Many companies developed their own sets of objects that could be used to support multiple languages. Data formatting was handled in a similar fashion.

JSTL includes tags that support both formatting and multilingual support, which provides a consistent approach to this common problem. Why is a consistent approach preferable? The most obvious reason is better compatibility with HTML generation tools. Multilingual JSP pages quickly break down most HTML generation tools. These tools simply can't see past the proprietary multilingual support and into the text that is behind them. When later versions of HTML editing tools are extended to recognize JSTL, better editing of multilingual JSP pages will result.

Another advantage of this uniformity is less training time for newly hired programmers. Normally, one of the first things that a new programmer must learn is how to construct JSP scriptlet code that is compatible with the company's multilingual and formatting support. If the company is using JSTL, it can expect that the programmer may have worked with JSTL formatting and multilingual support previously.

  • + Share This
  • 🔖 Save To Your Account