Consider once again the calculator from Listing 4.6. If the user enters large numbers—say, 1264528 and 9273912—the sum will be 10538440. This is certainly the right answer, but it is not in a particularly readable format. It would be much better if it could be displayed as 10,538,440. The issue of formatting comes up frequently when designing Web pages, as there are often particular rules about how numbers, currencies, and dates should be displayed.
Another portion of the standard library provides tags for displaying formatted values. This library can be imported by using
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
Once loaded, a number of new tags are available, some of which work similarly to the c:out tag but allow a format to be specified. For example, the c:out tag from Listing 4.6 could be replaced with
<fmt:formatNumber value="${calc.value1 + calc.value2}" pattern="###,###"/>
The pattern indicates that there should be a comma after every three digits. It would also be legal to provide a decimal point, as in ###,###.##, which would indicate that a comma should be placed every three digits, with two digits following the decimal point.
A number of examples have contained custom mechanisms for formatting dates. Now a more general solution to this problem is available: the fmt:formatDate tag. This tag works very much like fmt:formatNumber tag but expects its value to be a date. If the bean from Listing 3.1 were loaded with jsp:useBean, the date property could be formatted with
<fmt:formatDate value="${bean1.date}" pattern="hh:mm:ss MM/dd/yy"/>
The valid expressions for pattern can be found in the documentation for the java.text.SimpleDateFormat class, but note for the moment that any of the expressions from Listing 3.2 would work.
The formatting tags can do a great deal more than has been shown here. Different countries have different standard ways to express numbers and dates, and the format tags can ensure that data is formatted in an appropriate way for each country, through a mechanism called internationalization. The format tags can also be used to parse values, which would allow the calculator to accept inputs with commas and decimal points. These topics are beyond the scope of this book, but now that the basic functionality of these tags is clear, interested readers can see the remaining details in section 9 of the JavaServer Pages Standard Tag Library specification, which is available from http://java.sun.com/products/jsp/.