Trainers' thoughts on Java and related technologies

In previous articles, I said that the Model/View/Controller (MVC) design pattern is a driving force in application development. I demonstrated how JavaServer Pages (JSP) help mitigate MVC obstacles that dynamic Web page development often presents, but the handful of convenience tags native to JSP do not cover all the bases. In short, we might still need to introduce Java code into documents where programmatic logic is required. How do we get rid of that code completely?

JSP Taglibs go a long way to answer that question. JSP TagLibs are custom, HTML-style elements that define reusable JSP tags that represent some complex behavior. That behavior might be as simple as JSP markup, but it can also include JavaScript and calls to Java classes and their methods. That means you can extract common functionality from a JSP page, define it in a TagLib and reuse it in other pages as easily as reusing a familiar HTML tag. (In a future article, I’ll show you how to create custom tags.)

One of the most popular open source TabLibs is the JavaServer Pages Standard Tag Library (JSTL). It is now included with the Java EE Web application development platform. The JSTL extends the JSP specification by adding tag libraries for common tasks, such as XML and SQL processing, conditional and looping execution, form handling, internationalization and formatting. The JSTL was developed under the Java Community Process (JCP) as JSR 52 and released in 2002. The current version (JSTL 1.2) was released May 8, 2006.

The JSTL provides an effective way to embed logic within a JSP page without using embedded Java code directly. The use of a standardized tag set, rather than breaking in and out of Java code leads to more maintainable code and enables separation of concerns between the development of the application code and the user interface.

While the JSTL is commonly referred to as a single tag library, it is actually composed of four separate libraries:

  1. Core
  2. XML
  3. SQL
  4. Internationalization/formatting

Let’s consider an example. Assume I want to develop a JSP that displays the contents of a shopping cart in a table. We could use a scriptlet as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<table>
<%
   Iterator i = cart.getItems().iterator();
   while (i.hasNext()) {
      ShoppingCartItem item = ShoppingCartItem)i.next();
%>
   <tr>
      <td align="right" bgcolor="#ffffff">
         ${item.quantity}
      </td>
   </tr>
<%
   }
 %>
</table>

Now consider the markup the JSTL might use to do the same thing.

1
2
3
4
5
6
7
8
9
<table>
   <c:forEach var="item" items="${sessionScope.cart.items}">
      <tr>
         <td align="right" bgcolor="#ffffff">
            ${item.quantity}
         </td>
      </tr>
   </c:forEach>
</table>

The difference between the two is more than cosmetic. The JSTL approaches the job in a more View-oriented manner then the scriplet. Granted, they both do looping logic, but the JSTL fits more comfortably with HTML and is easier to read and therefore easier to maintain. The benefit of JSTL illustrated here grows as the complexity of the page increases. In my next article I’ll go into the details of how to implement TagLibs in a JSP.

See Introduction to JSP, Comprehensive JSP, Advanced JSP, and JSTL Training with WebLogic for some of our classes that cover JSP and JSTL.

No TweetBacks yet. (Be the first to Tweet this post)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

© Webucator, Inc. All rights reserved. | Toll Free: 877-932-8228 | UK: 0808-101-3484 | From outside the USA: 315-849-2724 | Fax: 315-849-2723