Modify the Help Class FacultyBean to Make It a Java Bean Class – Develop Java Web Applications to Access Databases

8.1.5.1   Modify the Help Class FacultyBean to Make It a Java Bean Class

First we need to create a new Java Session Bean class named FacultyBean in the NetBeans IDE. Then we need to add seven setter() methods into this bean class. Your finished Java bean class, FacultyBean.java, is shown in Figure 8.19. All modified code is in bold.

Let’s have a closer look at this piece of modified code to see how it works.

FIGURE 8.20   The code for the FacultyBeanQuery.jsp page.

From step A to step G, seven setter() methods are added into this Java bean class. All of these setter() methods are used to setup the initial values for the seven properties in this bean.

Next we need to create a new transaction JSP page FacultyBeanQuery.jsp to transfer data between our new starting page FacultyBeanPage.jsp and our Java bean class FacultyBean. java. Basically, the FacultyBeanQuery.jsp file has no significant difference from the FacultyQuery.jsp we built in the last section. The only different part is the way to execute the JDBC-and database-related queries or business logic. In the FacultyQuery.jsp file, we called a Java help class, FacultyBean.java, to do those functions. However, in FacultyBeanQuery.jsp, we will call a mod-ified help class that has been converted to a Java bean, FacultyBean.java, to perform these functions.

The code for the FacultyBeanQuery.jsp file is shown in Figure 8.20.

Now let’s have a closer look at this piece of code to see how it works.

Some system- or user-related packages are imported at the beginning of this page. The JSP directive<%@ page /> is used to convert those packages and embedded in this page. Three packages are imported here: the java.util.* package contains the List class, the JavaWebHibDBOraclePackage contains our Java bean class FacultyBean and the csedept.entity.Faculty is a Hibernate class mapping for the Faculty Table in our sample database, CSE _ DEPT.

A. The Java bean class is declared with the JSP tag with the three tags we discussed at the beginning of this section. The referenced name for this bean is dbFac-ulty, which is assigned to the id of the bean. The scope of this bean is session, and the full name of the bean class is JavaWebHibDBOraclePackage.FacultyBean.

B. The setProperty tag is used to setup all parameters passed from the FacultyBeanPage.jsp page to the associated properties in the bean class FacultyBean.

C. The Java code starting from the JSP tag and faculty name parameter is retrieved by using the getParameter() method and assigned to a local String variable, fname.
D. The main bean method, QueryFaculty(), is executed to query a faculty record based on the retrieved faculty name from the FacultyBeanPage.jsp page. The result is assigned to a local List variable. In fact, this result is not important in this application since the columns in the query result have been assigned to the associated properties in the bean class, and later on we can pick up those columns by calling the getter() methods in the bean class.
E. Since we want to fill those text fields in our starting page FacultyBeanPage.jsp with the queried result, we used the sendRedirect() method to return the process back to that page.

Now let’s take a look at a new starting page, FacultyBeanPage.jsp, that will be used to call the transaction JSP page and Java bean to perform the faculty data query and display the query result in this page. Because of the complexity of building this page with HTML code, we leave this coding job to our project development stage later.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024