Develop the Code to Call Our Web Service Project 2 – Develop Java Web Services to Access Databases

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

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

A. An additional system method, getParameter(), is used to get the name of the selected faculty image, which is located at the Faculty _ Image tag in the view class Faculty. jsp. The name of the selected faculty image will be automatically assigned and stored to the Faculty _ Image tag in the view class as soon as the image is selected.

B. A new String array, fnew, is created, and it is used to hold eight pieces of new faculty information stored in the eight local String variables.
C. 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.

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

E. 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 this insertion or insert another faculty record. A for() loop is used for that purpose.

Now let’s do our code for the Java managed bean class FacultyMBean.java.
Open our Web-based client project, WebClientFaculty _ Insert, and double-click on the FacultyMBean.java in the Projects window to open the managed bean class file. Let’s develop the code for the Insert() method in this class to fulfill this data insertion function.

Browse to the Insert() method and enter the code, as shown in Figure 9.51, into this method.

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

A. Some local variables, such as an integer variable numInsert and an ArrayList instance al, are created. The first variable is used to hold the run result of this data insertion, and the second variable is used to pick up and reserve the input new faculty data array.

B. The clear() method is executed to make sure that the ArrayList instance is clean before a new faculty record is collected.

C. The add() method is used to pick up and add eight pieces of new faculty information into this new ArrayList instance al. Eight pieces of new faculty information are entered and selected by the user in the JSP page Faculty.jsp and stored in eight tags defined in the view class Faculty.jsp page.

D. A try-catch block is used to call the data insertion operation InsertFaculty() defined in our Web Service project. First a service instance with its service port is generated.

E. The InsertFaculty() operation in our Web service is called with the ArrayList instance that contains eight pieces of new faculty information as the argument. The exe-cution result of this faculty data insertion is returned and assigned to the local Boolean variable insert.

F. If the returned Boolean variable insert is false, which means that this data insertion fails, a system method, System.out.println(), is used to indicate this.

G. The catch block is used to catch any possible exception during the data insertion process.
H. Finally the run result of this data insertion is returned to the calling method.

Now let’s build and run our Web client project to call our Web service operation to perform the faculty data insert action.

FIGURE 9.51   The modified code for the Insert() method.

Develop the Code to Call Our Web Service Project – Develop Java Web Services to Access Databases

9.10.2  Develop the Code to Call Our Web Service Project

To call our Web Service project to insert a new faculty record into the Faculty Table in our sample database, we need to perform the following code modification and development:

1) Modify the view class Faculty.jsp file to add a File Selection function to enable users to select a new faculty image to be inserted into the database.

2) Modify the control class FacultyProcess.jsp page to direct the insert query to the related operational method defined in the Java Bean class FacultyMBean. java file.

3) Create and add code into the model class FacultyMBean.java to call the related opera-tion, InsertFaculty(), defined in our Web Service to perform data insertion actions.

Let’s handle these tasks one by one in the following section. First let’s modify the view class Faculty.jsp by adding a File Selection function. Refer to Section 8.5.1 in Chapter 8 to perform this modification. For your convenience, we highlight the related modifications for that page in this part again. Perform the following steps to add this function:

1) Open the Faculty.jsp page and scroll down this file to find one of the input tags, which should be around line 381:
2) Enter the following tag and locate it just above the tag shown previously.

Your modified Faculty.jsp page should match the one shown in Figure 9.49. The new added tag part is in bold.

Next let’s modify the controller class FacultyProcess.jsp file to direct the insert query to the related method defined in that class.

Open the controller class file, FacultyProcess.jsp, and perform the following modifica-tions, as shown in Figure 9.50, to that file.

FIGURE 9.49   The modified code for the Faculty.jsp page.

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

1) Move 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 from the keyboard to get a new line under the else if block. Keep the cur-sor in that new line location.

2) Open the Palette window by going to Window > IDE Tools > Palette item. 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 into the ID field and webclient.FacultyMBean into the Class field. Select session from the Scope combo box. Click on the OK button to close this dialog box.

4) Add a JSP directive just under the previous JSP directive to set up all properties on the Java bean class FacultyMBean.java:

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