Modify the FacultyProcess.jsp Page to Handle Faculty Data Collection and Insertion – Develop Java Web Applications to Access Databases

8.5.2.2  Modify the FacultyProcess.jsp Page to Handle Faculty Data Collection and Insertion

Double-click on the FacultyProcess.jsp page in the Projects window in our new project, and perform the following modifications and additions to this page to use the Java bean class file

FIGURE 8.82   The finished Insert Use Bean dialog box.

FacultyInsertBean.java to perform a new faculty record insertion:

1) Move the cursor to the else if (request.getParameter(“Insert”)!= null) block; then type a JSP ending tag, %>, under the else if statement; and click on the Enter key on the keyboard to get a new line under the else if block.

2) Open the Palette window by going to Window > IDE Tools > Palette. In the opened Palette window, browse to the JSP tab, expand it and drag the Use Bean icon and place it under the JSP ending tag,%>.

3) In the opened Insert Use Bean dialog, enter InsertFaculty in the ID field and JavaWebOracleSelectPackage.FacultyInsertBean in the Class filed. Select session from the Scope combo box. Your finished Insert Use Bean dialog is shown in Figure 8.82. Click on the OK button to close this dialog box. A JSP directive that contains the bean id, bean scope and class is added to this block.

4) Add a JSP directive just under the previous JSP directive to setup all properties for the Java bean class FacultyInsertBean.java shown subsequently:

5) Add an opening JSP directive,<%, to start our Java code, to be built in the following.

The code related to steps 1~5 is shown in steps 1~4 in Figure 8.83. Add the Java code shown in steps A~G in Figure 8.83 into this block.

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

A. A local integer variable, res, is created and used to hold the run result of executing the InsertFaculty() method in the Java bean class FacultyInsertBean with the bean id of InsertFaculty.

B. Eight getParameter() methods are used to pick up eight pieces of new inserted faculty information stored in the eight fields in the Faculty.jsp page. The eight collected pieces of new faculty information are assigned to eight local String variables.

C. A new String array, fnew, is created and used to hold eight pieces of new faculty informa-tion stored in the eight local String variables.

D. The InsertFaculty() method defined in our Java bean is executed to insert these eight pieces of faculty information as a new faculty record into the Faculty Table. The eight pieces of new faculty information are stored in the String array fnew that works as the argument for this method. The run result of this method is returned and assigned to the local integer variable res.

FIGURE 8.83   The modified and added code for the Insert block in FacultyProcess.jsp.

E. If the run result is 0, it means that no record has been inserted into the Faculty Table, and this data insertion action fails. In that case, we need to re-display the Faculty.jsp page to enable users to re-insert that faculty record.

F. If the run result is non-zero, it means that the new faculty record has been inserted into the Faculty Table. We need to clean up all seven fields that contain seven pieces of new inserted faculty information in the Faculty.jsp page to enable users to either test the insertion or insert another faculty record. A for() loop is used for that purpose.

G. Also, we need to re-display the Faculty.jsp page to enable users to perform the next action.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *