A common and frustrating problem that many programmers encounter when first working with JSPs is how to access the size of a Collection using the JavaServer Pages Standard Tag Library (JSTL) and the Expression Language (EL). This article discusses this problem and illustrates a best practice solution.

The Problem

The JSTL and EL greatly reduce the amount of Java code in a JSP and as a result, can be used improve its maintainability. The EL provides a syntax for working with Java objects and allows an object’s properties to be accessed using a simple dot notation. So, for example, in order to access the size of a collection, we might assume that we could use an expression like this:
Unfortunately this will not work. In order to access a property in this way the object to which the property belongs must be implemented as a JavaBean component and it’s methods must adhere to the conventions described in the JavaBeans specification.
In short, to read the size property of a collection, the EL requires that the collection implements a getSize method; the bad news for us is that no such method exists in the Collections API.

Hack 1 – Use Scriptlets

The most common work-around to this problem is to simply fall back to using Java in a scriptlet in the JSP instead of the EL and to cast the request attribute to a collection:
This work-around produces some quite ugly code and becomes even worse when we use it as part of another expression; for example, to test the size within an if tag:
1.<c:if test="<%= ((Collection) request.getAttribute("collectionName")).size() == 1 %>">

Hack 2 – Provide a JavaBeans Compliant Collection Wrapper

A more elegant solution is to wrap the Collection with an object that implements the Collection interface and delegates all calls to the real collection:
01.public class JSTLCollectionWrapper implements Collection {
02.//the underlying collection that is being wrapped
03.private final Collection realCollection;
06.public JSTLCollectionWrapper(Collection c) {
07.realCollection = c;
10.//implement Collection interface and delegate all calls to realCollection
12.//provide a JavaBeans getter method for the size property
13.public int getSize() {
14.return realCollection.size();
Then in your controller class, wrap all collections before adding them to the request or the users session:
1.Collection wrapped = new JSTLCollectionWrapper(realCollection);
2.request.setAttrubute("collectionName", wrapped);
The JSTL expressions can now access the size property of the collection as the wrapper class implements this according to the JavaBeans specification:
The downside to this approach is that it requires you write the wrapper class, and that you must remember to use it wherever you add a collection as an attribute to an HTTP request or session in your controller classes.

The Best Practice Approach

Just so long as you’re using an application server that supports JSTL 1.1 or later (and you really should be), there’s a much neater solution available: use the functions tags.
The functions tags can be used in a JSP just like any other tag library, but rather than providing tags to use, the library extends the capabilities of the EL by providing some simple functions for you to use. Here’s how you can check the size of a collection using the functions tags:
1.<%-- declare that we intend to use the functions taglib --%>
2.<%@ taglib prefix='fn' uri='http://java.sun.com/jsp/jstl/functions' %>
4.<%-- use it --%>
The first line here is just a standard taglib directive telling the application server that we would like to use the functions tags. The second line illustrates how the length function can then be used to return the size of a collection.
As you can see, the syntax is pretty simple, far less intrusive than using a scriptlet, and does not require any special coding in your controller classes. It also has the added advantage that it works for arrays too, and will even return the length of a String should you need to figure that out.
This solution retains much of its simplicity even when used in more complex expressions:
So there you have it. If you’ve been trying to figure out how to work with collection’s sizes in your JSPs, or maybe you’ve just been praying that Sun would rename the size method to getSize, then the functions tag library can offer a neat and simple solution to this common programming pitfall.