Chapter 1. Introduction

   

Topics in This Chapter

  • JSTL Overview

  • Getting Started

  • A Simple JSTL Web Application

  • JSTL Design Principles

Since its introduction in 1995, Java has become the programming language of choice for developing e-business applications. [1] One of the main reasons for Java's success is its extensive class library, which lets developers implement complex applications with relative ease. Until now, server-side Java developers have had little in the way of a server-side class library beyond the servlet and JavaServer Pages (JSP) APIs. But as three major JSP specifications come to fruition in the latter part of 2002 ”JSP 2.0, JavaServer Faces, and the JSP Standard Tag Library (JSTL) ”server-side Java developers will suddenly be standing on a very broad set of shoulders.

[1] According to a study by the Cutter Consortium in November, 1999: see http://www. cutter .com/press/991130.html

This book is an in-depth examination of JSTL, which offers two invaluable capabilities that substantially simplify the implementation of JSP-based Web applications: an expression language and a comprehensive set of fundamental JSP tags (hereafter known as actions).

Before JSTL, JSP's most glaring weakness was, ironically, it's raison d ' etre: the use of JSP expressions and scriptlets to intersperse Java code with HTML. For example, if you have a color preference bean in session scope, you can access that bean's foreground color with this JSP expression:

 <%= ((beans.ColorPreferences)pageContext.       getAttribute("user", PageContext.SESSION_SCOPE)).      getForeground() %> 

The preceding expression accesses a bean through the JSP pageContext object and invokes the bean's getForeground method. That Java code is contained in a JSP expression, so the output is sent to the current JspWriter .

JSP scriptlets and expressions, such as the expression in the preceding code, make JSP pages more difficult to understand and maintain. They also require knowledge of the Java programming language, which narrows the field of JSP programmers. As if that weren't enough, JSP scriptlets and expressions provide a direct conduit to business logic. As long as your business logic is implemented in the Java programming language, developers can access it with reckless abandon from JSP pages. Mixing presentation and business logic makes applications more difficult to maintain and extend.

With the JSTL expression language and the <c:out> action, the previous JSP expression can be written like this:

 <c:out value='${user.foreground}'%> 

To say that the preceding code fragment is more user friendly than the preceding JSP expression involves a good dose of understatement. The expression language does not allow direct invocation of an object's methods , but it does allow you to specify a property name , which the expression language converts to an appropriate JavaBeans-compliant getter method; for example, the preceding code fragment results in a call to user.getForeground() .

The expression language alone is a vast improvement for JSP; in fact, it's such a crucial capability that it will be incorporated into the JSP 2.0 specification, which will be finalized in the latter part of 2002. But JSTL offers more than just an expression language ”its comprehensive suite of actions will make JSP development much easier and greatly reduce the reinvention of the wheel for everyday JSP tasks . For example, before JSTL, the common task of database access was not included in JSP's repertoire , so a developer could look for an existing database tag library or implement his own. That all changes with JSTL because it supports a set of database actions. The following JSP page shows how easy it is to make a database query and display the query results in an HTML table:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  <html>    <head>       <title>Accessing Database Queries</title>    </head>    <body>       <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>       <%@ taglib uri='http://java.sun.com/jstl/sql'  prefix='sql'%>       <%-- Execute a database query --%>       <  sql:query  var='customers'>          SELECT * FROM CUSTOMERS       </sql:query>       <%-- Access the rowCount property of the query --%>       <p>There are <  c:out  value='${customers.rowCount}'/> rows        in the customer query. Here they are:</p>       <%-- Create a table with column names and row data --%>       <p><table border='1'>          <tr>             <%-- Create table headers --%>             <  c:forEach  var='columnName'                      items='${customers.columnNames}'>                <th><  c:out  value='${columnName}'/></th>             </c:forEach>          </tr>          <%-- Create table rows and data --%>          <  c:forEach  var='row' items='${customers.rowsByIndex}'>             <tr>                <  c:forEach  var='rowData' items='${row}'>                   <td><  c:out  value='${rowData}'/></td>                </c:forEach>             </tr>          </c:forEach>       </table>    </body> </html> 

The preceding JSP page executes an SQL query that selects all customers in a database. Subsequently, the JSP page uses the <c:forEach> action to iterate over column names and row data to create an HTML table that displays the query result. If you want to read more about the preceding JSP page and see the output that it produces, see "Querying a Database" on page 378.

Now that we have a basic understanding of JSTL and how we can use it, let's take a closer look at what JSTL is and what it can do for you.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net