JAVA RUNTIME OBJECTS METHOD
9.13 BUILD A WEB - BASED CLIENT PROJECT TO CONSUME
We can still use a Web - based client project WebClientSQL we built in Section 9.7 to consume our Web service to perform the faculty data updating and deleting actions. First, let ’ s refresh the Web service reference used for our Web - based client project to allow it to use the updated Web service operations.
9.13.1 Refresh the Web Service Reference for Our Web - Based Client Project
In order to call the UpdateFaculty() and DeleteFaculty() operations in our Web service project WebServiceSQL , we need to refresh the Web reference in our Web - based client project WebClientSQL to use the updated Web service project. Perform the following operations to refresh the Web service reference:
1. Open our Web - based client project WebClientSQL and expand the Web Service References node.
2. Right click on our Web service WebServiceSQLService and choose the Delete item to remove this old Web reference.
3. Right click on our Web - based client project WebClientSQL and select the New > Web Service Client item to open the New Web Service Client wizard.
4. On the opened wizard, click on the Browse button that is next to the Project fi eld and expand our Web application WebServiceSQLApp . Then choose our Web service WebServiceSQL by clicking on it, and click on the OK button.
5. Click on the Finish button to complete this Web service reference refreshing process.
Now that we have refreshed or updated the Web service reference for our Web - based client project WebClientSQL , next, let ’ s develop the codes in our client project to call that Web service operations UpdateFaculty() and DeleteFaculty() to perform faculty data updating and deleting actions.
First, let ’ s take care of the data updating operation UpdateFaculty() .
9.13.2 Develop the Codes to Call Our Web Service Operation UpdateFaculty()
The main coding process is in the Java managed bean class FacultyMBean.java . As we know, a binding relationship between the action attribute of the Update command Button in our JSF page FacultyPage.jsp and the Update() method in our Java managed bean class FacultyMBean.java has been established. Therefore, we can con- centrate on the coding for the Update() method in our Java managed bean.
Open our Web - based client project WebClientSQL , and double click on the
FacultyMBean.java from the Projects window to open this managed bean class fi le. Let ’ s do the coding for the Update() method in this class to fulfi ll this data updating function.
Browse to the Update() method and drag the Web service operation UpdateFaculty under the Web Service References node and place it inside the Update() method. A piece of codes is created and added into this method, as shown in Figure 9.63 .
It is unnecessary to explain the function of this piece of codes line by line since all of coding lines have been illustrated by the built - in comments.
Now let ’ s do some modifi cations to this piece of codes and add some codes to meet our data updating requirements. Enter the codes that are shown in Figure 9.64 into this method.
Figure 9.63. The automatically created codes by dragging the operation node.
A B C D E
public String Update() {
try { // Call Web Service Operation
org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();
// TODO initialize WS operation arguments here java.util.List<java.lang.Object> fdata = null;
// TODO process result here
java.lang.Boolean result = port.updateFaculty(fdata);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
return null;
}
c09.indd 835
c09.indd 835 7/20/2011 11:12:52 AM7/20/2011 11:12:52 AM
www.traintelco.com
Let ’ s have a closer look at this piece of newly added codes to see how it works.
A. First, a new ArrayList instance al is created and initialized. This variable is used to pick up and reserve the input updating faculty data array.
B. The clear() method is executed to make sure that the ArrayList instance is clean before a updating faculty record is collected.
C. The add() method is used to pick up and add six pieces of updating faculty information into this new ArrayList instance al . Six pieces of updating faculty information are entered by the user in the JSF page FacultyPage.jsp and stored in six properties defi ned in this managed bean. The last parameter, the seventh one, is the original faculty name.
D. The UpdateFaculty() operation in our Web service is called with the ArrayList instance that contains six pieces of updated faculty information as the argument. The execution result of this faculty data updating is returned and assigned to the local Boolean variable
update .
E. If the returned Boolean variable update is false, which means that this data updating has failed, the msgDlg instance is used to indicate this situation.
F. The catch block is used to catch any possible exception during this data updating process.
G. Finally, a null is returned since it is not important to our application.
Next, let ’ s build the codes for the Delete() method in our managed bean FacultyMBean.
java to call our Web service operation DeleteFaculty() to perform the faculty data delet- ing action.
Figure 9.64. The modifi ed codes for the Update() method.
A B C
D E
F
G
public String Update() {
ArrayList al = new ArrayList();
MsgDialog msgDlg = new MsgDialog(new javax.swing.JFrame(), true);
al.clear();
al.add(0, name);
al.add(1, office);
al.add(2, phone);
al.add(3, college);
al.add(4, title);
al.add(5, email);
al.add(6, facultyName);
try { // Call Web Service Operation
org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();
// TODO initialize WS operation arguments here Boolean update = port.updateFaculty(al);
if (!update) {
msgDlg.setMessage("The data updating is failed!");
msgDlg.setVisible(true);
}
} catch (Exception ex) {
// TODO handle custom exceptions here msgDlg.setMessage("exception: " + ex);
msgDlg.setVisible(true);
}
return null;
}
9.13.3 Develop the Codes to Call Our Web Service Operation DeleteFaculty()
As we know, a binding relationship between the action attribute of the Delete command Button in our JSF page FacultyPage.jsp and the Delete() method in our Java managed bean class FacultyMBean.java has been established. Therefore, we can concentrate on the coding for the Delete() method in our Java managed bean.
Open our Web - based client project WebClientSQL and double click on the
FacultyMBean.java from the Projects window to open this managed bean class fi le.
Let ’ s do the coding for the Delete() method in this class to fulfi ll this data deleting function.
Browse to the Delete() method and drag the Web service operation DeleteFaculty under the Web Service References node and place it inside the Delete() method. A piece of codes is created and added into this method, as shown in Figure 9.65 .
It is unnecessary to explain the function of this piece of codes line by line since all of coding lines have been illustrated by the built - in comments.
Now let ’ s do some modifi cations to this piece of codes and add some codes to meet our data deleting requirements. Enter the codes that are shown in Figure 9.66 into this method.
Let ’ s have a closer look at this piece of new added codes to see how it works.
A. First a MsgDialog instance msgDlg is created, and this instance is used to track and display any possible exception during the data deleting action.
B. The DeleteFaculty() operation in our Web service is called with the original faculty name as the argument. The execution result of this faculty data deleting is returned and assigned to the local Boolean variable delete .
C. If the returned Boolean variable delete is false, which means that this data deleting has failed, the msgDlg instance is used to indicate this situation.
D. The catch block is used to catch any possible exception during this data deleting process.
E. Finally, a null is returned since it is not important to our application.
Now, let ’ s build and run our Web client project to call our Web service operations to perform the faculty data updating and deleting actions.
Figure 9.65. The automatically created codes by dragging the operation node.
A B C D E
public String Delete() {
try { // Call Web Service Operation
org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();
// TODO initialize WS operation arguments here java.lang.String fname = "";
// TODO process result here
java.lang.Boolean result = port.deleteFaculty(fname);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
return null;
}
c09.indd 837
c09.indd 837 7/20/2011 11:12:52 AM7/20/2011 11:12:52 AM
www.traintelco.com
9.13.4 Build and Run Our Client Project to Update and Delete Faculty Record via Web Service
Click on the Clean and Build Main Project button to build our client project. If every- thing is fi ne, right click on our Web - based client project WebClientSQL from the Projects window and choose the Deploy item to deploy our Web application. Then, right click on our JSF page FacultyPage.jsp from the Projects window and choose the Run File item to run our client project.
On the opened JSF page, fi rst, let ’ s perform a faculty record query by entering a desired faculty name such as Ying Bai into the Faculty Name fi eld, and then click on the Select button to get details for this selected faculty member. To update this faculty record, enter six pieces of updating faculty information shown below into the associated six text fi elds, as shown in Figure 9.67 .
Click on the Update button to try to call our Web service operation UpdateFaculty() to update this faculty record in the Faculty table in our sample database.
To confi rm this data updating action, two methods can be used. First, we can open our Faculty table using either the Services window in the NetBeans IDE or the SQL Server 2008 Management Studio to check whether this faculty record has been updated. The second way to confi rm this data updating, which is simpler, is to use the Select button in this form to perform a query to try to retrieve the updated faculty record.
• Name: Susan Bai • Title: Professor • Offi ce: MTC - 200 • Phone: 750 - 378 - 2000 • College: Duke University • Email: sbai@college.edu
Figure 9.66. The modifi ed codes for the Delete() method.
A
B C
D
E
public String Delete() {
MsgDialog msgDlg = new MsgDialog(new javax.swing.JFrame(), true);
try { // Call Web Service Operation
org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();
// TODO initialize WS operation arguments here
Boolean delete = port.deleteFaculty(facultyName);
if (!delete) {
msgDlg.setMessage("The data deleting is failed!");
msgDlg.setVisible(true);
}
} catch (Exception ex) {
// TODO handle custom exceptions here msgDlg.setMessage("exception: " + ex);
msgDlg.setVisible(true);
} return null;
}
The second way to do this checking is to, fi rst, perform another query for the selected faculty such as Jenney King , and then go to the Faculty Name fi eld and type the updated faculty name Susan Bai into this fi eld. Click on the Select button to try to retrieve it. Now you can fi nd that six pieces of updated faculty information for the updated faculty member Susan Bai have been retrieved and displayed in this page, as shown in Figure 9.68 .
Now let ’ s test the faculty deleting action by calling our Web service operation
DeleteFaculty() . First, let ’ s perform another faculty updating action to recover the faculty
Figure 9.67. Six pieces of updated faculty information.
Figure 9.68. The confi rmation of an updated faculty record.
c09.indd 839
c09.indd 839 7/20/2011 11:12:52 AM7/20/2011 11:12:52 AM
www.traintelco.com
member Ying Bai ’ s record. Enter six pieces of original information shown in Table 9.7 into six associated fi elds in this page, and click on the Update button to complete this data updating.
Now type the updated faculty name Ying Bai into the Faculty Name fi eld and click on the Select button to retrieve this updated faculty record. Then click on the Delete button to try to delete this faculty record.
To confi rm this data deleting action, click on the Select button again to try to retrieve this faculty record from our sample database. An exception message is displayed to indi- cate that no matched faculty can be found from our sample database. Our data deleting is successful!
Sometimes, the execution of this deleting action seems to be still executed without completion. This means that an exception occurred. To watch this exception message, just minimize all current opened windows and forms, and then you can fi nd this message.
To make our sample database clean and neat, it is highly recommended to recover this deleted faculty member and related records in our Faculty, LogIn, Course, and StudentCourse tables. An easy way to do this recovery is to use the Microsoft SQL Server 2008 Management Studio. Refer to deleted records shown in Tables 9.2 – 9.5 in Section 9.11.3 to add or insert them back to the related tables to complete this data recovery.
Our Web client project to consume our Web service WebServiceSQL is successful!
A complete Web client project WebClientSQL can be found from the folder DBProjects\
Chapter 9 that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ).
Next, let ’ s discuss how to build a Web service to access and manipulate data against our sample Oracle database.