The JSP Standard Tag Library (JSTL) is a collection of tag libraries encapsulating common functions in JSP pages.
Starting with Java EE 5, JSTL 1.2 is available for you to use without having to perform any additional tasks. The Web container takes care that JSTL is available in the Web application classpath and you can use the JSTL libraries once you import them in your JSP pages and tag files. If you bundle the JSTL libraries with a Web application, the Web container ignores them because libraries provided by the platform always take precedence over libraries bundled with applications.
To use the JSTL libraries, you import the library you wish to use with the taglib directive.
JSTL Libraries by Area
Area |
Subfunction |
Prefix |
URI |
Core |
Variable support Flow control URL management Miscellaneous |
c |
http://java.sun.com/jsp/jstl/core |
XML |
XML Core XML Flow control XML Transformation |
x |
http://java.sun.com/jsp/jstl/xml
|
I18n |
Locale Message formatting Number and date formatting |
fmt |
http://java.sun.com/jsp/jstl/fmt
|
Database |
SQL |
sql |
http://java.sun.com/jsp/jstl/sql |
Functions |
Collection length String manipulation |
fn |
http://java.sun.com/jsp/jstl/functions |
● Evaluating expressions
Similarly to JSP expressions such as <%= scripting-language-expression %> or ${el-expression}, in JSTL, you use the c:out action to evaluate an expression and output the result of the expression to the screen.
Your Web application implements a shopping cart and you print the number of items in a customer shopping cart:
You have <c:out value="${sessionScope.user.itemCount}"/> items.
By default, <c:out> converts the characters <, >, ', ", & to their corresponding character entity codes (that is, < is converted to < , and so on). This conversion ensures that the JSP page is displayed correctly in the browser window and prevents security risks. To bypass this conversion, you specify the action’s escapeXml attribute with a value of false.
● Setting values of JSP scoped attributes
You set the variable var1 to the value of myValue:
<c:set var="var1">
<my:myTag>myValue</ my:myTag >
</c:set>
Then you can use the variable in another tag:
<my:myTag2 var1="${var1}"/>
You can also use <c:set> to set the property of a JavaBeans object or add or set a specific element in a java.util.Map object.
<!-- set property in JavaBeans object -->
<c:set target="${cust.address}" property="city" value="${city}"/>
<!-- set/add element in Map object -->
<c:set target="${preferences}" property="color" value="${param.color}"/>
You can also use <c:set> to set a deferred-value that can later be evaluated by a tag handler. In such cases, you do not need to specify a scope.
<!-- set deferred value -->
<c:set var="d" value="#{handler.everythingDisabled}"/>
<h:inputText id="i1" disabled="#{d}"/>
<h:inputText id="i2" disabled="#{d}"/>
● Executing a database query and displaying the result
You use <sql:query> to create a query to a database and <c:forEach> to display the result. Using <c:forEach> you repeat nested body content over a collection of objects, or repeat nested body content a fixed number of times.
You create a query to retrive all customers from China and order the results by the lastname column:
<sql:query var="customers" dataSource="${dataSource}">
SELECT * FROM customers
WHERE country = ’China’
ORDER BY lastname
</sql:query>
You then create an HTML table to display the result and use the forEach action to display all retrieved records. The forEachaction generates a table row for each displayed record.
<table>
<c:forEach var="row" items="${customers.rows}">
<tr>
<td><c:out value="${row.lastName}"/></td>
<td><c:out value="${row.firstName}"/></td>
<td><c:out value="${row.address}"/></td>
</tr>
</c:forEach>
</table>
● URL rewriting and encoding
To rewrite or encode a URL where necessary, you use the <c:url> and <c:param> actions together. You use <c:url> to rewrite a URL if necessary, and <c:param> to transparently encode query string parameters (both name and value).
<c:url value="http://mysite.com/go/register" var="myUrl">
<c:param name="name" value="${param.name}"/>
<c:param name="country" value="${param.country}"/>
</c:url>
<a href=’<c:out value="${myUrl}"/>’>Register</a>
● Including the content of a file in a JSP
To include the content of a disclaimer readme file in your JSP, you use the c:import action. To specify the location of the file, you specify the url attribute and enter the URL of the file as a value.
<c:import url="ftp://ftp.mysite.com/README"/>