GETTING STARTED WITH JAVA WEB SERVICES USING NETBEANS IDE – Develop Java Web Services to Access Databases

9.4  GETTING STARTED WITH JAVA WEB SERVICES USING NETBEANS IDE

In the following sections, we will develop and build different Java Web services projects based on our Oracle Database XE 18c database system with different consuming projects to perform desired database operations.

By adding different operations or methods to our Web service projects to access and manipulate data in Tables, such as the Faculty and Course Tables, in our sample Oracle database, we can perform the following data queries and manipulations:

1) Query data from the Faculty Table in our Oracle database with QueryFaculty().
2) Insert data into the Faculty Table in our Oracle database with InsertFaculty().
3) Update and delete data against the Faculty Table in our Oracle database with the
UpdateFaculty() and DeleteFaculty() operations.
4) Query data from the Course Table in our Oracle database with QueryCourse().

FIGURE 9.19  The run result of calling our Web service.

5) Query detailed course information from the Course Table in our Oracle database with
DetailCourse().

6) Update and delete data against the Course Table in our Oracle database with the
UpdateCourse() and DeleteCourse() operations.

For each Web services project, we need to build an associated client project to consume the Web services project to test its function. The following client projects will be built:

1) Window-based client project to consume the Web service to access the Faculty and Course Tables in our Oracle database.
2) Window-based client project to consume the Web service to insert data into the Faculty and Course Tables in our Oracle database.

3) Window-based client project to consume the Web service to update and delete data against the Faculty and Course Tables in our Oracle database.
4) Web-based-based client project to consume the Web service to access the Faculty and Course Tables in our Oracle database.

5) Web-based-based client project to consume the Web service to insert data into the Faculty and Course Tables in our Oracle database.

6) Web-based-based client project to consume the Web service to update and delete data against the Faculty and Course Tables in our Oracle database.

In fact, we can develop any kind of client project to consume a Web service, either a standard Java desktop application, which is called a Window-based consume project, or a JSP page, which is called a Web-based consume project. We will develop and build different client projects to consume our Web services to enable our projects to meet real-world needs.

Let’s start with the query of the Faculty Table in our Oracle database.

Create a New Java SOAP-Based Web Service Project WebServiceFaculty – Develop Java Web Services to Access Databases

9.5.2  Create a New Java SOAP-Based Web Service Project WebServiceFaculty

The function of this Web service is to perform data queries and manipulations to our sample Oracle 18c XE database and return the result. Perform the following operations to create this new Web service project, WebServiceFaculty:

1) In the opened Projects window, right-click on our new created project, WebApp Faculty, and select the New > Other menu item to open the New File wizard.
2) Select Web Services from the Categories list and Web Service from the File Types list, and click on the Next button.
3) Name the Web service WebServiceFaculty and type org.ws.oracle into the
Package field. Leave Create Web Service from Scratch selected.

Your finished Name and Location wizard should match the one shown in Figure 9.22. Click on the Finish button to complete this process.

Next let’s handle adding new operations and code for the operations or methods in our Web service.

9.5.3  Add the First Web Operation to Our Web Services to Perform a Data Query

The main purpose of using the Web service in this section is to query data from the Faculty Table in our sample database; thus, we need to add a new operation, QueryFaculty(). Perform the fol-lowing steps to add QueryFaculty() into our Web service project:

1) Click on the Design button at the top of the window to open the Design View of our Web service project, WebServiceFaculty.java.
2) First let’s remove the default hello operation by right-clicking on it, and select Remove Operation item from the popup menu.

FIGURE 9.22  The finished Name and Location wizard.

FIGURE 9.23  The finished Add Operation wizard.

3) Click on the Add Operation button to open the Add Operation wizard.
4) Enter QueryFaculty into the Name field and click on the Browse button next to the Return Type combo box. Type ArrayList into the Type Name field, select the item ArrayList (java.util) from the list and click on the OK button.

5) Click on the Add button and enter fname into the Name parameter field. Keep the default type java.lang.String unchanged and click on the OK button to complete the new operation creation process.

Your finished Add Operation wizard should match the one shown in Figure 9.23.
Click on the Source button at the top of this window to open the code window of our Web ser-vice project. Let’s develop the code for this new operation.
In the opened code window, enter the code shown in Figure 9.24 into the new operation. Let’s have a closer look at this piece of code to see how it works.

BUILD A WINDOW-BASED CLIENT PROJECT TO CONSUME THE WEB SERVICE – Develop Java Web Services to Access Databases

9.6  BUILD A WINDOW-BASED CLIENT PROJECT TO CONSUME THE WEB SERVICE

To save time and space, we can use the Window-based project OracleSelectFaculty we developed in Section 6.3 in Chapter 6 to build our new client project, WinClientFaculty _ Select. That project can be found in the folder Class DB Projects\Chapter 6, which is located in the Students folder on the CRC Press ftp site (refer to Figure 1.2 in Chapter 1).

9.6.1  Copy the FacultyFrame and MsgDialog Components as GUIs

Perform the following operations to create a GUI for our Window-based client project,
WinClientFaculty _ Select, to consume our Web service:

1) Launch NetBeans IDE 12.0 and choose File > New Project.
2) Select Java with Ant and Java Application from the Categories and
Projects lists, respectively. Click on the Next button.
3) Name the project WinClientFaculty _ Select and select a desired folder to save this project. Uncheck the Create Main Class checkbox. Your finished Name and Location wizard should match the one shown in Figure 9.31. Click on the Finish button to create this project.

FIGURE 9.31   The finished Name and Location wizard.

FIGURE 9.32   The finished Copy Class wizard.

4) Go to the Students folder on the CRC Press ftp site and load and open the project OracleSelectFaculty from the folder Class DB Projects\Chapter 6.
5) In the opened project, right-click on the Faculty Frame file FacultyFrame.java under the project package node, and select the Refactor > Copy item to copy this form file.

Copy Class—FacultyFrame wizard, select our new project,
WinClientFaculty _ Select, from the Project combo box and remove the 1 FacultyFrame from the New Name field. Your finished Copy Class wiz-6)Intheopenedafterthe ard is shown in Figure 9.32.
7) Click on the Refactor button to make a refactored copy of the frame file.
8) Return to our new project, WinClientFaculty _ Select, and you can see that a copied FacultyFrame.java file has been pasted in the default package in our project.
Perform a similar Refactor operation to copy the MsgDialog.java file and paste it into our new client project. Next let’s develop the code to call our Web service to perform the faculty data query. However, before we can begin the coding process, we must first setup or create a Web service reference for our WinClientFaculty _ Select project to enable our project to recognize the Web service and call it when it is instructed to do so.

Create a Web Service Reference for Our Window-Based Client Project – Develop Java Web Services to Access Databases

9.6.2  Create a Web Service Reference for Our Window-Based Client Project

Perform the following operations to set up a Web service reference for our client project:

1) Build and deploy our Web Service application project, WebAppFaculty, first to make sure that the Web Service is available to our client project.

2) Right-click on our client project WinClientFaculty _ Select in the Projects window, and select the New > Other item to open the New File wizard.

3) In the opened New File wizard, select Web Services from the Categories and Web Service Client from the File Types list, respectively. Click on the Next button to continue.

4) Click on the Browse button for the Project field and expand our Web application proj-ect WebAppFaculty, and click on our Web service WebServiceFaculty to select it. Then click on the OK button to select this Web service. Your finished Web Service Client wizard should match the one shown in Figure 9.33.

5) Click on the Finish button to complete the Web service reference setup process.

Immediately, you can see a new node named Web Service References has been created and added to our client project. Expand this node, and you can see the associated Web service port and our Web service operations, such as QueryFaculty() and QueryImage(), under that node.

FIGURE 9.33  The finished New Web Service Client wizard.

Now let’s develop the code to call the Web service to perform the data query from the Faculty Table in our sample database.

9.6.3  Develop the Code to Call Our Web Service Project

The coding process is divided into two parts: the modification to the original code and creation of new code. First let’s do some modifications to the original code in the FacultyFrame class. Perform the following code modifications to make this project our Web consuming project:

1) Double-click on our new copied FacultyFrame.java file from our project to open it.
2) Click on the Source button at the top to open the code window.

3) Go to the constructor of this class, and remove all three query methods from the ComboMethod object.

4) Open the SelectButtonActionPerformed() event handler and remove all code inside this event handler.

Now let’s develop some new code to perform the faculty data query by calling our Web service operations.

In the Design view of the FacultyFrame form window, double-click on the Select button to open its event method, SelectButtonActionPerformed(). Then enter the code shown in Figure 9.34 into this method. The new added and modified code is in bold.
Let’s have a closer look at this piece of code to see how it works.

A. Some useful packages and classes related to Java development, such as image, imageio, swing and File, are imported first since we need to use some related classes defined in those packages to perform data and image queries and display in this Form.

B. A new ArrayList instance, al, is created to receive and hold the query result.
C. A try-catch block is used to call our Web service to perform the faculty data query operation. First a new Web service instance, service, is created based on our Web ser-vice class, WebServiceFaculty _ Service. Starting with NetBeans IDE 7, the syntax to create a new service has changed, and an underscore is used between the service class name and the Service keyword to represent the service class. Similarly, a service port is also generated by calling a system method, getWebServiceFacultyPort(), to get the current port used by our Web service. This port is returned and assigned to a new port instance, port.

FIGURE 9.34   The modified code for the SelectButtonActionPerformed() method.

Modify Three Files to Make Them Work for Our Web Client Project 2 – Develop Java Web Services to Access Databases

Finally, let’s modify our model class FacultyMBean.java. First let’s concentrate on the QueryFaculty() method. The function of this method is to:

1) Call our Web service operation QueryFaculty() to pick up a matching faculty record from the Faculty Table in our sample database.
2) Assign each queried column to the associated property defined in our Java managed bean class FacultyMBean.java.

Open the file FacultyMBean.java and perform the modifications as shown in Figure 9.38. The modified lines are in bold. This is the first part of the code of this class file. The second part’s code will be shown later. Let’s have a closer look at this piece of code to see how it works.

A. The class name is changed from FacultyQuery to FacultyMBean.

B. The type of the faculty image object is changed from the Blob to the byte[[] array.
C. A new instance of our Web Service class is generated with a new Web Service port.
D. Our second operation in our Web Service project, QueryImage(), is executed to get the selected faculty image and assign it to the image byte array fbimg, which is used later.

Also, delete the whole body for the method CloseDBConnection() from this Java Bean class. Now let’s take a look at our second part of code for this model class file, which is shown in Figure 9.39. The modified parts are in bold. In fact, only the contents of one method, getFacul-tyImage(), are modified. The function of this method is to convert the faculty image file from the byte[] array to a specified image format, base64Image, to enable it to be displayed in the Faculty Image box in our view class Faculty.jsp page. The setBase64Image() method is to set this converted image to the property base64Image defined in this class, which can be used by the
image source tag in our view class later.

FIGURE 9.38   The completed code for the QueryFaculty() method.

At this point, we have completed all modifications for our three files. Before we can continue to build our project to call our Web Service, that is, to call related operations in our Web Service project to perform faculty data query from our sample database, we need to add our Web Service as a reference to our client project to enable the latter to recognize the former.

Now let’s add a Web reference to our current Web-based client project to enable our client to know our Web service and its operations.

Modify Three Files to Make Them Work for Our Web Client Project – Develop Java Web Services to Access Databases

9.7.3  Modify Three Files to Make Them Work for Our Web Client Project

We need to modify the following three class files to make them work for our client project:

1) Modify our view class, Faculty.jsp, to display the selected faculty image and to termi-nate our Web client project when the Back button is clicked by the user.

2) Modify our control class, FacultyProcess.jsp, to make decisions to perform appro-priate operations by calling related methods in our Java Bean class or our model class, and setup the correct mapping between our view and model classes.

3) Modify our Java Bean or model class, FacultyMBean.java, to handle all operations required by our control class by calling related Web service operations.

First let’s modify our view class Faculty.jsp file. Open this file and go to codeline 127, where the image source tag is assigned by the selected faculty image. Change that line from

src=“data:image/jpg;base64,${FacultyQuery.base64Image}” to src=“data:image/jpg;base64,${FacultyMBean.base64Image}”

since our new created Java Bean class is named FacultyMBean, not FacultyQuery, which was used by the previous project, JavaWebOracleSelect, we built in Chapter 8.

The next modification is to change one code line in the bottom of the JSP file, Faculty.jsp, to safely terminate our Web consume client project. Open this JSP file Faculty.jsp and browse to the bottom of that file, around line 520. Replace the original line:

with the following line:

The reason for this change is because the Faculty.jsp page in our previous projects will be returned to the Selection.jsp page if this Back button is clicked by the user, but in this Web Client project, WebClientFaculty _ Select, we only rebuild the Faculty.jsp page without using any other pages; thus, we need to modify the code in the Back button event handler to terminate this project.

Now let’s modify the FacultyProcess.jsp file to make it our new control class:

1) Replace the import package name <%@ page import=“JavaWebDBJSPSQLPackage

.” %> with our new package <%@ page import=“webclient.” %> in codeline 10.

2) Replace the original constructor line (line 20), FacultyQuery fQuery = new FacultyQuery(); with our new Java Bean class FacultyMBean fQuery = new FacultyMBean();.

3) Go to code line 40 and change the FacultyQuery to FacultyMBean, and the result of that code line should be:

request.setAttribute(“FacultyMBean”, fQuery);

4) Remove the entire block else if (request.getParameter(“Back”)!= null) {} located at the bottom, at lines 122–125, since we do not want to use the Back button to return to any other page.

Build and Run Our Client Project to Query Faculty Data via Web Service – Develop Java Web Services to Access Databases

9.7.5   Build and Run Our Client Project to Query Faculty Data via Web Service

Click on the Clean and Build Main Project button to build our client project. If every-thing is fine, click on the Run Project button on the top (green arrow) to run our client project.

On the opened Web page, which is shown in Figure 9.41, enter a desired faculty name such as Ying Bai into the Faculty Name field. Then click the Select button to perform a query for this selected faculty member. The query result is returned and displayed in this page, as shown in Figure 9.41. Click on the Back button to terminate this project if you like.

Our Web client project used to consume our Web service WebServiceFaculty is success-ful! A complete Web client project, WebClientFaculty _ Select, can be found in the project folder Class DB Projects\Chapter 9 in the Students folder on the CRC Press ftp site (refer to Figure 1.2 in Chapter 1).

Next, let’s discuss how to build a Web service to perform data insertion into our sample Oracle database.

9.8  BUILD JAVA WEB SERVICES TO INSERT DATA INTO THE ORACLE DATABASE

To perform a faculty record insertion to our sample database using our Web service, we need to add another operation called InsertFaculty() into our Web service project, WebServiceFaculty.

FIGURE 9.41   The test result for our Web client project.

9.8.1  Add a New Operation InsertFaculty() into Our Web Service Project

Perform the following operations to add this operation into our Web service:

1) Launch NetBeans IDE 12.0 and open our Web application project, WebAppFaculty, and open our Web service main class file, WebServiceFaculty.javainthe Projects window.
2) Click on the Design button at the top of the window to open the Design View of our Web service class file WebServiceFaculty.java.
3) Click on the Add Operation button to open the Add Operation wizard.
4) Enter InsertFaculty into the Name field and click on the Browse button that is next to the Return Type combo box. Type boolean into the Type Name field, select the item Boolean (java.lang) from the list and click on the OK button.

5) Click on the Add button and enter fdata into the Name parameter field. Then click on the dropdown arrow of the Type combo box and select the Choose item to open the Find Type wizard. Type Arraylist into the top field and select the ArrayList (java.util) data type, and click on the OK button to select an ArrayList as the data type for the input parameter.

Your finished Add Operation wizard should match the one shown in Figure 9.42. Click on the OK button to complete the new operation creation process.

Click on the Source button on the top of this window to open the code window of our Web service class file. Let’s build the code for this new added operation.
In the opened code window, enter the code shown in Figure 9.43 into this new added operation, InsertFaculty().
Let’s have a closer look at this piece of code to see how it works.

A. First a local integer variable, numInsert, is created, and it is used to hold the run result of inserting a new faculty record into our sample database.

FIGURE 9.42  The complete Add Operation wizard.

Refresh the Web Service Reference for Our Window-Based Client Project 2 – Develop Java Web Services to Access Databases

FIGURE 9.45   The code for the Insert button event handler.

H. The add() method is used to pick up and add eight pieces of new faculty information into this new ArrayList instance, al. These eight pieces of new faculty information are entered by the user and stored in seven text fields and a File Chooser in the FacultyFrame window form. The toString() method is used to convert each piece of new faculty information obtained using the getText() method that returns an object data type to a String. The index is necessary since it is used to indicate the position of each parameter in this ArrayList. One point to be noted is the order of adding these text fields, which must be identical to the order of data columns in our Faculty Table.
I. A try-catch block is used to call our Web service operation, InsertFaculty(), to perform the faculty data insert action. First a new Web service instance, service, is created based on our Web service class, WebServiceFaculty _ Service. Then the getWebServiceFacultyPort() method is executed to get the current port used by our Web service. This port is returned and assigned to a new port instance, port.

J. The Web service operation InsertFaculty() is executed with the ArrayList instance al that has been filled with eight pieces of new faculty information as the argument of this method. The run result of that operation is returned and assigned to a Boolean variable, insert.

K. If the value of the variable insert is false, which means that no row has been inserted into our Faculty Table and this insertion has failed, the msgDlg instance is used to show this situation.

L. Otherwise, if the value of the insert variable is true, which means that this data inser-tion is successful, the new inserted faculty name will be added into the Faculty Name combo box ComboName using the addItem() method.

M. The catch block is used to track and display any possible exception during the Web ser-vice operation execution.

Now we are ready to build and run our client project to test its function.

Refresh the Web Service Reference for Our Window-Based Client Project – Develop Java Web Services to Access Databases

9.9.1  Refresh the Web Service Reference for Our Window-Based Client Project

In order to call the InsertFaculty() operation in our Web service project, WebService Faculty, we need to refresh the Web reference in our Window-based client project to use the updated Web service project. Perform the following operations to refresh the Web service reference:

1) Build and deploy our Web Service application, WebAppFaculty, to make it available.
2) Open our Window-based client project, WinClientFaculty _ Insert, and expand
the Web Service References node.
3) Right-click on our Web service, WebServiceFaculty, and choose the Delete item to remove this old Web reference.
4) Right-click on our Window-based client project, WinClientFaculty _ Insert, and select the New > Web Service Client item to open the New Web Service Client wizard.
5) In the opened wizard, click on the Browse button next to the Project field and expand our Web application, WebAppFaculty. Then choose our Web service, WebServiceFaculty, by clicking on it, and click on the OK button.
6) Click on the Finish button to complete the Web service reference refresh process.

Now that we have refreshed or updated the Web service reference for our Window-based client project WinClientFaculty _ Insert, next let’s develop the code in our client project to call the Web service operation InsertFaculty() to perform faculty data insertion.

9.9.2  Develop the Code to Call Our Web Service Project

Open the Window-based client project WinClientFaculty _ Insert and double-click on our main class, FacultyFrame.java, to open it. Click on the Design button to open the graphic user interface. In this client project, we want to use the Insert button in this form as a trigger to start the faculty data insertion action. Therefore, double-click on the Insert button to open its event method, InsertButtonActionPerformed(), and enter the code shown in Figure 9.45 into this method.
Let’s have a closer look at this piece of code to see how it works.

A. Some local objects are first declared, which include a File object, imgFile, which is used to hold the selected image file, and a new ArrayList instance, al, which is used to pick up and reserve the input new faculty data array.

B. A Java File Chooser instance, imgChooser, is generated, and it provides a file interface GUI to allow users to select a desired faculty image to be inserted into our database later.

C. The current directory, which is the folder of our current project, is selected, and this loca-tion will work as a default folder to store all selected faculty image files later.

D. The system method, showOpenDialog(), is executed to display the Chooser GUI to enable users to browse and select a desired faculty image to be inserted into our database later.
E. By checking one property, APPROVE _ OPTION, one can confirm whether the GUI oper-ation is successful. If it is opened and an image has been selected, a true is returned. Then the selected image can be retrieved by calling a method, getSelectedFile().

F. These two code lines are used to display the path and directory for the selected faculty image, and they are used for debugging purposes.

G. The clear() method is executed to make sure that the ArrayList instance is clean and empty before a new faculty record can be assigned to it.

BUILD A WEB-BASED CLIENT PROJECT TO CONSUME THE WEB SERVICE – Develop Java Web Services to Access Databases

9.10  BUILD A WEB-BASED CLIENT PROJECT TO CONSUME THE WEB SERVICE

We can still use the Web-based client project we built in Section 9.7, WebClientFaculty _ Select, to consume our Web service to perform the faculty data insertion action. But we prefer to modify that project and make it our new Web-based client project WebClientFaculty _ Insert. Let’s copy that project and make it our new project. Perform the following steps to create our new project, WebClientFaculty _ Insert:

1) In the Projects window, right-click on the project WebClientFaculty _ Select and select the Copy item from the popup menu to open the Copy Project wizard.

2) Enter our new project name, WebClientFaculty _ Insert, into the Project Name box; browse to the default project folder, C:\Class DB Projects\Chapter 9, as the Project Location; and click on the Copy button.

A new project, WebClientFaculty _ Insert, is generated and added into our Projects window.

First let’s add a Web service reference to our Web-based client project to allow it to use our Web service operations.

9.10.1  Refresh the Web Service Reference for Our Web-Based Client Project

In order to call the InsertFaculty() operation in our Web service project, WebAppFaculty, we need to refresh the Web reference in our Web-based client project, WebClientFaculty _ Insert, to use the updated Web service project. Perform the following operations to refresh the Web service reference:

1) Build and deploy our Web Service application project, WebAppFaculty, first.
2) Open our Web-based client project, WebClientFaculty _ Insert, and expand the Web Service References node.
3) Right-click on our Web service, WebServiceFaculty, and choose the Delete item to remove this old Web reference.

4) Right-click on our Web-based client project, WebClientFaculty _ Insert, and select the New > Web Service Client item to open the New Web Service Client wizard.

5) In the opened wizard, click on the Browse button next to the Project field and expand our Web application, WebAppFaculty. Then choose our Web service project, WebServiceFaculty, by clicking on it, and click on the OK button.

6) Click on the Finish button to complete the Web service reference refreshing process.

Now that we have refreshed or updated the Web service reference for our Web-based client proj-ect, WebClientFaculty _ Insert, next let’s develop the code in our client project to call the Web service operation InsertFaculty() to perform faculty data insertion.