Java data access

Page 311

Chapter 17: Building Data−centric Web Applications Figure 17−10: The All Employee Web page — take 2 This completes my discussion of the first of three actions provided by this example application. The other two actions and their associated JSP pages are Delete Employee and Insert Employee, and they work in much the same way. Like Edit Employee, each has an intermediate page that solicits user input. Each also has a confirmation page that performs the designated action. You will find both of these on the book’s Web site. Although the previous JSP application works, it’s far from optimized. For example, the same code to interact with the database is duplicated across pages. Simply put, redundant code creates a maintenance nightmare. If you find a bug in one page then you must not only fix it there, but also propagate the change across all the other pages that use the same code. A simple solution to this problem is to put all the redundant code in one place and reference it across all pages. You then only have to maintain one codebase and reuse it. When you do encounter a bug, one change may be sufficient to fix it for all pages that reference this code. Another example of non−optimized code is that each page request opens a database connection, performs one or more database actions, and then destroys the connection. (This is the same problem I described with regard to the servlet in the previous section.) You can remedy the situation by using the jspInit() and jspDestroy() methods provided to you by the JSP implementation class when the JSP container converts the JSP page into a servlet. There is a better alternative. The ideal solution to both of these problems is to encapsulate the common functionality into one class. This class will automatically handle the tasks of establishing and maintaining the necessary physical database connections, and of reusing them to attain the highest possible performance. Additionally, this class will provide you with convenient methods for performing common database functions. Furthermore, this class can be instantiated once per application to eliminate the overhead of creating multiple instantiations. A JavaBeans component is the type of class that can accommodate all of these requirements and is directly supported by JSP.

Using JSP with JDBC JavaBeans Using JavaBeans with your JSP pages is an ideal solution to the problem of redundant code and logic. In this section, I describe what JavaBeans are, how to develop them, and how to effectively use them in your JSP pages. I also present the bean I used to overcome the redundancy and performance problems I encountered developing my example application. What is a JavaBean? A JavaBeans component, commonly referred to simply as a bean, is a generic class that encapsulates information referred to as properties. JavaBeans are predominantly used to wrap GUI components, such as in AWT, in a generic adaptor class. Java IDEs use the bean wrapper to seamlessly integrate these components. You can easily construct the components using a default constructor or a constructor with no arguments. Furthermore, the bean wrapper provides you with access to the standard GUI properties so you can retrieve and set the values of component attributes such as width and background color. Note More information regarding JavaBeans can be found at http://java.sun.com/products/javabeans. However, JavaBeans can also wrap non−GUI components. For example, the javax.sql.RowSet interface acts like a ResultSet that has a bean wrapper. In this case, the properties you set affect connection parameters, cursor types, and transaction levels, not GUI properties. XRef 299


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.