JAVA RUNTIME OBJECTS METHOD
9.7 BUILD A WEB - BASED CLIENT PROJECT TO CONSUME THE
9.7.4 Build the Codes to Call the Web Service to Perform
First, let ’ s concentrate on the Select() method. The function of this method is to:
1. Call our Web service operation QueryFaculty() to pick up a matched faculty record from the Faculty table in our sample database.
2. Assign each queried column to the associated property defi ned in our Java managed bean class FacultyMBean.java .
Each queried column will be refl ected and displayed in the associated text fi eld in our JSF page FacultyPage.jsp since they have been bound together.
There are two ways available to develop the codes inside the Select() method to call our Web service operation QueryFaculty() to perform the faculty data query: (1) drag the Web service operation node from the Projects window, and drop it to inside the
c09.indd 805
c09.indd 805 7/20/2011 11:12:50 AM7/20/2011 11:12:50 AM
www.traintelco.com
Figure 9.37. The automatically added codes by dragging the operation node.
A B C D E
public String Select() {
try { // Call Web Service Operation
org.ws.sql.WebServiceSQLService service = new org.ws.sql.WebServiceSQLService();
org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();
// TODO initialize WS operation arguments here java.lang.String fname = "";
// TODO process result here
java.util.List<java.lang.Object> result = port.queryFaculty(fname);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
return null;
}
Select() method, and (2) right click on any place inside the Select() method, and select the Insert Code item and choose the Call Web Service Operation item from the pop - up menu.
Let ’ s use the fi rst method as an example to add the codes to call our Web service operation.
1. Expand the Web Service References node under our Web - based client project
WebClientSQL , and continue to expand the subservice port until our operation
QueryFaculty node.
2. Open the code window of our Java managed bean class FacultyMBean.java , and browse to the Select() method.
3. Drag our Web service operation QueryFaculty node and place it inside the Select() method in our managed bean.
A piece of codes is automatically created and added into this method, which has been highlighted in bold and shown in Figure 9.37 .
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 query requirements. Perform the following operations to make this piece of codes to perform our desired faculty data query:
A. An ArrayList instance al and a MsgDialog instance msgDlg are created fi rst. The fi rst local variable al is used to hold the returned query, and the second is used to display some debug information during the project runs.
B. Remove the code line java.lang.String fname = “”; .
C. Replace the rest of the codes with a piece of codes that are shown in steps C , D , E , and F in Figure 9.38 . First, the queryFaculty() operation in our Web service is executed to perform the faculty data query, and the result is returned and assigned to the local variable
al . One point to be noted is that this returned result must be casted with ArrayList class, since the ArrayList < String > data type is used for this query result in our Web service operation.
D. Seven returned columns are assigned to the associated properties defi ned in this managed bean FacultyMBean.java class, and will be displayed in the associated text fi eld in our JSF page FacultyPage.jsp , since each of those tags has been bound to each associated property.
The get() method is used to pick up each column from the returned query result, and a
toString() method is used to convert each column to a String and assigned each of them to the associated property.
E. The getter method getFacultyImage() is executed to pick up a matched faculty image and display it in the faculty image box in our JSF page FacultyPage.jsp . Refer to that getter method to get the detailed codes for this method defi ned in this Java managed bean.
F. The catch block is used to track and display any possible exception during this Web service operation calling process.
During the coding process, you may encounter some real - time compiling errors. Most of these errors are introduced by missing some packages that contain classes or compo- nents used in this fi le. To fi x these errors, just right click on this code window and select the Fix Imports item to load and import those missed packages to the top of this code window.
Now we have fi nished all coding process for this faculty data query action. Before we can build and run our project to test its function, we need to copy and save all images used in this project, including both faculty and students ’ image fi les, to our current project folder. Perform the following actions to fi nish this image fi les processing:
1. Open the Images folder that is located at the Wiley ftp site (refer to Figure 1.2 in Chapter 1 ), copy all image fi les from that folder.
Figure 9.38. The modifi ed codes for the method Select().
A
B C D
E F
public String Select() {
ArrayList al = new ArrayList();
MsgDialog msgDlg = new MsgDialog(new javax.swing.JFrame(), true);
try { // Call Web Service Operation
org.ws.sql.WebServiceSQLService service = new org.ws.sql.WebServiceSQLService();
org.ws.sql.WebServiceSQL port = service.getWebServiceSQLPort();
// TODO process result here
al = (ArrayList)port.queryFaculty(facultyName);
facultyID = al.get(0).toString();
name = al.get(1).toString();
office = al.get(2).toString();
phone = al.get(3).toString();
college = al.get(4).toString();
title = al.get(5).toString();
email = al.get(6).toString();
facultyImage = getFacultyImage();
} catch (Exception ex) {
// TODO handle custom exceptions here
msgDlg.setMessage("Exception in Query Faculty Table: " + ex);
msgDlg.setVisible(true);
} return null;
}
c09.indd 807
c09.indd 807 7/20/2011 11:12:50 AM7/20/2011 11:12:50 AM
www.traintelco.com
2. In the NetBeans IDE, open our project WebClientSQL and click on the Files button to open the Files window. Then, right click on the web node under our project WebClientSQL and select the Paste item to paste all image fi les into our current project node WebClientSQL . Now we are ready to build and run our client project to test its function.