Core JSTL[c] Mastering the JSP Standard Tag Library

   

Topics in This Chapter

  • Overview

  • A Simple Database

  • How JSTL Locates Data Sources

  • Creating Data Sources

  • Querying a Database

  • Updating a Database

  • Executing Database Transactions

  • Implementing Database Custom Actions

The vast majority of commercial websites are driven by relational databases. Because databases are so pervasive on the Web, JSTL provides a set of actions for database connectivity and access. The central theme of this chapter is how you can make the most of those actions.

First however, a cautionary note. Most often, it's desirable to separate presentation logic from business logic; for example, many Web applications are implemented with the Model-View-Controller (MVC) architecture, where business components manipulate a data model, and JSP pages present views of that data. [1] The JSTL SQL actions let you mix presentation and business logic as much as you want; for example, you can perform database updates with the <sql:update> action or execute database transactions with <sql:transaction>.

[1] See David Geary, Advanced JavaServer Pages , Prentice Hall, 2001, for more information about separating presentation and business logic.

Ultimately, the extent to which you separate presentation and business logic is up to you. You may decide to limit database access to business components and therefore forbid the use of JSTL SQL actions in JSP pages.

Another approach is to allow database queries in JSP pages (with the <sql:query> action) and only allow database updates and transactions in business components. This approach provides a good degree of separation between presentation and business logic without requiring business components to encapsulate query results and make them available to JSP pages.

Finally, you can manipulate your database entirely in JSP pages by using all of the JSTL SQL actions. If you choose that approach, be aware that your Web application may be somewhat difficult to maintain and extend because you will be mixing presentation and business logic in JSP pages.

This chapter begins with an overview of the JSTL SQL actions, followed by a short discussion of a simple database used throughout this chapter. After those preliminaries , this chapter discusses the following actions: connecting to databases, querying databases, updating databases, executing database transactions, and implementing custom actions.

   

Категории