Chapter 20. Developing Custom Tag Libraries

Custom actions let you encapsulate logic and make it available to page authors in a familiar format. Throughout this book, JSTL actions and a number of generic custom actions are used for such tasks as accessing a database, including localized content, encoding URLs, and much more. Using these actions, the amount of Java code in the JSP pages can be kept to a minimum, making the application easier to debug and maintain. However, for a complex application, these generic actions may not be enough. Perhaps you want to develop application-specific actions to access the database instead of putting SQL statements in the JSP pages. You may want to present complex data as a set of nested HTML tables with cells formatted differently depending on their values. Instead of using JSTL conditional and loop actions in the JSP page to generate this table, an application-specific custom action can be used.

A common use for a custom action is as an HTTP-specific adapter to a bean. JavaBeans components are frequently used in a web application, possibly by components in different environments such as servlets, applets, and JSP pages. Custom actions automatically get access to all information about the request, the response, and all the variables in the JSP scopes through the JSP API. A custom action can therefore extract the information using the JSP-specific API and feed the raw data to the bean, making it possible to reuse the bean in all environments. ...

Get JavaServer Pages, Second 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.