Introducing to JSP Standard Tag Library

JSP is designed for the presentation layer in web applications but it needs to contain the logic or code inside the page to control the way it presents the visual elements.

Since JSP was invented, scriptlet has been used intensively, therefore, the JSP pages become messy and difficult to maintain. The HTML mixed with JSP scriptlet and opening and closing brace makes the JSP page even hard to extend.

In June 2002, JavaServer Pages standard library (JSTL) specification 1.0 was first released.  JSTL provided new ways for JSP authors to work with different elements with standard friendly tags. The current version of the JSTL is 1.2 which was started in 2004 so we will use JSTL 1.2 for all the tutorials.

JSP standard tags library can be divided into four tag libraries as follows:

  1. Core tags
  2. Internationalization (i18l) and formatting tags
  3. Relational database access tags
  4. XML processing tags

The goals of those tag library above are:

  • Simplify the task of writing JSP page by providing friendly XML base tags
  • Provide reusable logic from page’s presentation
  • Make the JSP page easier to read and maintain

Core tags

As its name implies, core tags provide the core functionality actions to JSP to make the most common actions easier to achieve in a more effective way. Core tags specify several actions such as displaying content based on condition, manipulating collections and URL managing. By using the core tags you’ll never have to write such a small piece of scriptlet. (But you still need to know scriptlet to maintain legacy web applications and convert them to JSTL later on if you have to).

Internationalization(I18L) and formatting tags

Those tags specify a series actions to make the web application multilingual. Those actions including managing resource bundle, locales and base names.

Relational database access tags

Accessing a database is the most major task of web applications. JSTL provides a list of standard tags to help you to manipulate data such as select, insert, update and delete from the relational databases.

XML processing tags

XML becomes a standard in enterprise web applications for exchanging data. Manipulate XML effectively, therefore, is very important for most web applications, and of course, JSTL also provides a list of tags for processing from XML parsing to XML transformation.

Let’s take a look at an example of how we achieve the same JSP page with scriptlet and JSTL.

<%@page contentType="text/html" pageEncoding="UTF-8"%> <html>     <head>         <title>JSP scritlet</title>         <style type="text/css">             .odd{background-color:white}             .even{background-color:gray}         </style>     </head>     <body>         <table border="1" width="100px">             <% for (int c = 1; c < 10; c++) {                     if (c % 2 == 0) {             %>             <tr class="even">                 <td><%= c%></td>             </tr>             <% } else {%>             <tr class="odd">                 <td><%= c%></td>             </tr>             <%}             }%>         </table>     </body> </html>
Code language: JavaScript (javascript)
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri=""           prefix="c" %> <html>     <head>         <title>JSTL page</title>         <style type="text/css">             .odd{background-color:white}             .even{background-color:gray}         </style>     </head>     <body>         <table border="1" width="100px">             <c:forEach begin="1" end="10" step="1" var="c">                 <c:choose>                     <c:when test = "${c%2 ==0}">                         <tr class="even">                             <td><c:out value="${c}" /></td>                         </tr>                     </c:when>                     <c:otherwise>                         <tr class="odd">                             <td><c:out value="${c}" /></td>                         </tr>                     </c:otherwise>                 </c:choose>             </c:forEach>         </table>     </body> </html>
Code language: HTML, XML (xml)

As you can see the JSP page with JSTL looks more readable than the JSP page with scriptlet. You’ll those tags in the next tutorials.