JAVA RUNTIME OBJECTS METHOD
9.10 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 insertion action. First, let ’ s refresh
c09.indd 815
c09.indd 815 7/20/2011 7:59:13 PM7/20/2011 7:59:13 PM
www.traintelco.com
the Web service reference used for our Web - based client project to allow it to use the updated 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 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 operation InsertFaculty() to perform faculty data insertion.
9.10.2 Develop the Codes to Call Our Web Service Project 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 Insert com- mandButton in our JSF page FacultyPage.jsp and the Insert() method in our Java managed bean class FacultyMBean.java has been established. Therefore, we can con- centrate on the coding for the Insert() 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 Insert() method in this class to fulfi ll this data insertion function.
Browse to the Insert() method and drag the Web service operation InsertFaculty under the WebService References node and place it inside the Insert() method. A piece of codes is created and added into this method, as shown in Figure 9.46 .
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 insertion requirements. Enter the codes that are shown in Figure 9.47 into this method.
Let ’ s have a closer look at this piece of new 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 new faculty data array.
Figure 9.46. The automatically created codes by dragging the operation node.
A B C D E
public String Insert() {
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.insertFaculty(fdata);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
return null;
}
Figure 9.47. The modifi ed codes for the Insert() method.
A B C
D E
F
G
public String Insert() {
ArrayList al = new ArrayList();
MsgDialog msgDlg = new MsgDialog(new javax.swing.JFrame(), true);
al.clear();
al.add(0, facultyID);
al.add(1, name);
al.add(2, office);
al.add(3, phone);
al.add(4, college);
al.add(5, title);
al.add(6, email);
try { // Call Web Service Operation
org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();
// TODO initialize WS operation arguments here Boolean insert = port.insertFaculty(al);
if (!insert) {
msgDlg.setMessage("The data insertion is failed!");
msgDlg.setVisible(true);
}
} catch (Exception ex) {
// TODO handle custom exceptions here msgDlg.setMessage("exception: " + ex);
msgDlg.setVisible(true);
} return null;
}
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 seven pieces of new faculty information into this new ArrayList instance al . Seven pieces of new faculty information are entered by the user in the JSF page FacultyPage.jsp , and stored in seven properties defi ned in this managed bean.
D. The InsertFaculty() operation in our Web service is called with the ArrayList instance that contains seven pieces of new faculty information as the argument. The execution result of this faculty data insertion is returned and assigned to the local Boolean variable insert .
c09.indd 817
c09.indd 817 7/20/2011 11:12:50 AM7/20/2011 11:12:50 AM
www.traintelco.com
Figure 9.48. Seven pieces of new inserted faculty information.
E. If the returned Boolean variable insert is false, which means that this data insertion 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 insertion process.
G. 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 operation to perform the faculty data inserting action.
9.10.3 Build and Run Our Client Project to Insert Faculty Data 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 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 insert a new faculty record, enter seven pieces of new faculty information shown below into the associated seven text fi elds, as shown in Figure 9.48 .
• Faculty ID: T56789 • Name: Tom Jeff • Title: Professor • Offi ce: MTC - 150 • Phone: 750 - 378 - 1500 • College: University of Miami • Email: tjeff@college.edu
Figure 9.49. The confi rmation of a new faculty record insertion.
Click on the Insert button to try to call our Web service operation InsertFaculty() to insert this new faculty record into the Faculty table in our sample database.
To confi rm this data insertion, two ways can be used. The fi rst way is to open our Faculty table using either the Services window in the NetBeans IDE or the SQL Server 2008 Management Studio to check whether this new faculty record has been inserted.
The second way to confi rm this data insertion, which is simpler, is to use the Select button in this form to perform a query to try to retrieve the inserted faculty record.
The second way to do this checking, fi rst, you can perform another query for the selected faculty, such as Ying Bai , and then go to the Faculty Name combo box and type the new inserted faculty name Tom Jeff into this box. Click on the Select button to try to retrieve it. Now you can fi nd that seven pieces of new inserted faculty informa- tion have been retrieved and displayed in this page, as shown in Figure 9.49 .
It is highly recommended to remove this new inserted faculty record from our data- base since we want to keep our database clean. You can delete this record by opening the SQL Server 2008 Management Studio to do it.
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 perform data updating and deleting against our sample SQL Server database.