Utility Beans

A utility bean performs some action, such as processing information, as opposed to simply acting as a container for information.

The UserInfoBean contains processing code in addition to the plain property setter and getter methods, namely the validation and test code. The way the bean is used in this book, it’s perfectly okay to keep the validation code in the bean itself. However, let’s say you would like to add a property that references another bean, a friends property for instance, that holds an array of other UserInfoBean objects. It may then be better to let a utility bean that knows about all users in the application perform the validation, including verifying that the friends exist.

A bean used for validation is one example of a utility bean you can use to make the application easy to maintain. The CatalogBean used in Chapter 10 is another example. The version developed for this book simply creates a set of ProductBean objects with hardcoded values and provides a method that returns all products in the catalog. In a real application, it would likely get the information from a database instead and have methods for updating catalog information, such as adding and removing products or changing the information about a product, as well as methods that return only the products matching various search criteria. If all catalog update requests go through the CatalogBean, it can create, delete, and update the ProductBean objects so that they always match the information ...

Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.