JAVA RUNTIME OBJECTS METHOD
9.16 BUILD A WEB - BASED WEB CLIENT PROJECT
9.16.4 Develop the Codes to Call Our Web Service Project
Let ’ s start from the fi rst method Select() defi ned in our managed bean CourseBean to query all course_id based on the selected faculty member.
9.16.4.1 Build Codes for the Select Button Method to Query CourseIDs
The function of this method is to query all course_id taught by the selected faculty member from the Course table when the Select button is clicked by the user.
The queried result is returned and displayed in a Select - One - Listbox in the CoursePage.
jsp page.
First, let ’ s add our Web service operation QueryCourseID() into this Select() method by performing the following operations:
1. Open the code window of our Java managed bean CourseBean.java and browse to the
Select() method.
2. Browse to the Web Service References node under our client project
WebClientOracle .
3. Expand our Web service until all our fi ve Web service operations have been exposed.
4. Drag the QueryCourseID operation and place it inside the Select() method.
5. A piece of codes is automatically created and added into this method, which is shown in Figure 9.127 .
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 course record query requirements. Enter the codes that are shown in Figure 9.128 into this method, and the modifi ed codes have been highlighted in bold.
Table 9.12. The relationship between each bean method and each operation
Method in CourseBean Web Service Operation Function
Select() QueryCourseID() Query all course_id taught by the selected faculty Details() QueryCourse() Query detailed information for selected course_id Update() UpdateCourse() Update an existing course record in the Course table
Delete() DeleteCourse() Delete a course record from the Course table
Let ’ s have a closer look at this piece of modifi ed codes to see how it works.
A. Two ArrayList instances are created fi rst since we need to use them to perform the
course_id query and store queried result.
B. The Web service operation QueryCourseID() is called to get all course_id taught by the selected faculty member that works as an argument for this operation. The queried result is returned and assigned to the fi rst ArrayList instance cList .
C. A for loop is used to pick up each queried course_id and add it into the courseList Listbox in our CoursePage.jsp page. Here, a tricking issue is that you must convert the courseList that is a List instance to an ArrayList object, and then you can use the add() method to add all queried course_id into this courseList since the List is an abstract class. Otherwise, you may encounter some null reference exception when your project runs.
Figure 9.127. The automatically created and added codes.
A B C D E
public String Select() {
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
// TODO initialize WS operation arguments here java.lang.String fname = "";
// TODO process result here
java.util.List<java.lang.Object> result = port.queryCourseID(fname);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here }
return null;
}
Figure 9.128. The modifi ed codes for the Select() method.
A
B C
D
E
public String Select() {
ArrayList<String> cList = new ArrayList<String>();
courseList = new ArrayList();
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
// TODO process result here
cList = (ArrayList)port.queryCourseID(getFacultyName());
for (int col = 0; col < cList.size(); col++) {
SelectItem courseid = new SelectItem(cList.get(col).toString());
courseList.add(courseid.getValue());
}
} catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
} return null;
}
c09.indd 895
c09.indd 895 7/20/2011 11:12:55 AM7/20/2011 11:12:55 AM
www.traintelco.com
D. The catch block is used to track and display any possible exception during this course_id query process.
E. Finally, a null is returned. Since we never use this returning value in this application, it is not important to us.
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 the course_id query action. Let ’ s build and run our Web - based client project to test its function. Click on the Clean and Build Main Project button to build our client project. If everything is fi ne, deploy our client project by right clicking on our client project WebClientOracle and choose the
Deploy item.
Run our client project by right clicking on our JSF page CoursePage.jsp from the
Projects window and choose the Run File item.
On the opened JSF page, which is shown in Figure 9.129 , enter a desired faculty name, such as Ying Bai, into the Faculty Name fi eld. Then click the Select button to query all course_id taught by this selected faculty member. The query result is returned and displayed in the courseList Listbox on this page, as shown in Figure 9.129 .
Our Web client project to consume our Web service operation QueryCourseID() is successful! A complete Web client project WebClientOracle 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 consume the Web service operation QueryCourse() to get detailed course information for a given course_id from our sample Oracle database.
Figure 9.129. The course_id queried result for the selected faculty member.
9.16.4.2 Build Codes for the Detail Button Method to Get Course Details Now let ’ s do the coding for the Details() method in our Java managed bean CourseBean.
java to call a Web service operation QueryCourse() to get detailed course information for a given course_id .
The function of this method is to query detailed course information for a given
course_id via the Web service operation QueryCourse() as the Details button in our JSF page CoursePage.jsp is clicked by the user. The queried result is returned and dis- played in fi ve text fi elds in that page.
First, let ’ s add our Web service operation QueryCourse() into this Details() method by performing the following operations:
1. Open the code window of our Java managed bean CourseBean.java and browse to the
Details() method.
2. Browse to the Web Service References node under our client project WebClientOracle . 3. Expand our Web service until all our fi ve Web service operations have been exposed.
4. Drag the QueryCourse operation and place it inside the Details() method.
5. A piece of codes is automatically created and added into this method.
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 course details query requirements. Enter the codes that are shown in Figure 9.130 into this method, and the modifi ed codes have been highlighted in bold.
Let ’ s have a closer look at this piece of modifi ed codes to see how it works.
A. An ArrayList instance al is created fi rst since we need to use it to collect and store the result of querying the detailed course information from our Course table.
B. We need to check whether a valid course_id has been selected by the user from the CourseList Listbox in our JSF page. If a valid course_id has been chosen, the Web service
Figure 9.130. The modifi ed codes for the Details() method.
A
B C
D
E
public Boolean Details() {
ArrayList<String> al = new ArrayList<String>();
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
if (selectedItem != null) {
al = (ArrayList)port.queryCourse(selectedItem);
courseName = al.get(1).toString();
credit = al.get(2).toString();
classroom = al.get(3).toString();
schedule = al.get(4).toString();
enrollment = al.get(5).toString();
}
} catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
} return null;
}
c09.indd 897
c09.indd 897 7/20/2011 11:12:55 AM7/20/2011 11:12:55 AM
www.traintelco.com
operation QueryCourse() is called to get detailed course information for that selected
course_id (whose value has been bound to the selectedItem property) that works as an argument for this operation. The queried result is returned and assigned to the ArrayList instance al .
C. A group of get() methods is used to pick up each piece of course information and assign it to the associated property in this managed bean. Since each property has been bound to the associated value attributes of each inputText fi eld in our JSF page CoursePage.jsp , each piece of detailed course information will be displayed in each of those inputText fi elds.
The fi rst element whose index is 0 in the returned query result is course_id .
D. The catch block is used to track and display any possible exception during this course details query process.
E. Finally, a null is returned. Since we never use this returning value in this application, it is not important to us.
Now we have fi nished all coding process for the course details query. Let ’ s build and run our Web - based client project to test its function. Click on the Clean and Build Main Project button to build our client project. If everything is fi ne, deploy our client project by right clicking on our client project WebClientOracle and choose the Deploy item.
Run our client project by right clicking on our JSF page CoursePage.jsp from the
Projects window and choose the Run File item.
On the opened JSF page, which is shown in Figure 9.131 , enter a desired faculty name, such as Ying Bai , into the Faculty Name fi eld. Then click the Select button to query all course_id taught by this selected faculty member. Then click on any course_id for which you want to get detailed information from the CourseList Listbox. The detailed course information for the selected course_id is retrieved and displayed in fi ve inputText fi elds, as shown in Figure 9.131 .
Our Web client project to consume our Web service operation QueryCourse() is successful!
Figure 9.131. The running result of querying details for the course CSE - 438.
Next, let ’ s discuss how to consume the Web service operation UpdateCourse() to update an existing course record in our Course table for a given faculty member.
9.16.4.3 Build Codes for the Update Button Method to Update Courses Now let ’ s do the coding for the Update() method in our Java managed bean CourseBean.
java to call a Web service operation UpdateCourse() to update an existing course record for a given faculty member.
The function of this method is to update a course record for a given faculty member via the Web service operation UpdateCourse() as the Update button in our JSF page
CoursePage.jsp is clicked by the user.
First, let ’ s add our Web service operation UpdateCourse() into this Update() method by performing the following operations:
1. Open the code window of our Java managed bean CourseBean.java and browse to the
Update() method.
2. Browse to the Web Service References node under our client project WebClientOracle . 3. Expand our Web service until all our fi ve Web service operations have been exposed.
4. Drag the UpdateCourse operation and place it inside the Update() method.
5. A piece of codes is automatically created and added into this method.
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 course updating requirements. Enter the codes that are shown in Figure 9.132 into this method, and the modifi ed codes have been highlighted in bold.
Figure 9.132. The modifi ed codes for the Update() method.
A B C
D E F
G
public Boolean Update() { boolean update = false;
ArrayList al = new ArrayList();
al.clear();
al.add(0, selectedItem);
al.add(1, courseName);
al.add(2, credit);
al.add(3, classroom);
al.add(4, schedule);
al.add(5, enrollment);
al.add(6, getFacultyName());
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
update = port.updateCourse(al);
if (!update)
System.out.println("Error in UpdateCourse()...");
} catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
} return null;
}
c09.indd 899
c09.indd 899 7/20/2011 11:12:55 AM7/20/2011 11:12:55 AM
www.traintelco.com
Let ’ s have a closer look at this piece of modifi ed codes to see how it works.
A. Two local variables, update and al , are created fi rst. The fi rst one is a Boolean variable used to hold the running result of the execution of the Web service operation
UpdateCourse() , and the second is an ArrayList instance used to store a updating course record to be updated in the Course table in our sample database.
B. The ArrayList instance al is cleaned up using the clear() method to make sure that the al is empty before it can store any data.
C. A group of add() methods are used to add six pieces of updated course information into the ArrayList instance (The fi rst parameter is a course_id whose value is stored in the
selectedItem property that works as an updating criterion and will not be updated). One point to be noted is that the order in which to add these course parameters must be identi- cal with the order of assigning these parameters to the Course object in the session bean method setCourse() in our Web service project WebServiceOracle . Refer to that method to make sure that both orders are identical.
D. The Web operation UpdateCourse() is called to update this existing course record via our Web service. The execution result that is a Boolean variable is returned and assigned to the local variable update .
E. If a false is returned, which means that this course data updating has been failed, the system println() method is used to indicate this situation.
F. The catch block is used to track and display any possible exception during this data updat- ing process.
G. Finally, a null is returned. Since we never use this returning value in this application, it is not important to us.
Now we have fi nished all coding process for the course data updating action. Let ’ s build and run our Web - based client project to test its function. Click on the Clean and Build Main Project button to build our client project. If everything is fi ne, deploy our client project by right clicking on our client project WebClientOracle and choose the
Deploy item.
Run our client project by right clicking on our JSF page CoursePage.jsp from the
Projects window and choose the Run File item.
On the opened JSF page, which is shown in Figure 9.133 , enter a desired faculty name, such as Jenney King , into the Faculty Name fi eld. Then click the Select button to query all course_id taught by this selected faculty member. Immediately, you can fi nd that all four courses or four course_id taught by this faculty have been returned and displayed in the CourseList Listbox. Click on the Details button to get details for this selected course_id . To update an existing course CSC - 233B, keep this course_id selected from the CourseList Listbox and enter fi ve pieces of updated course information shown below into fi ve text fi elds.
• Course: Network Theory • Schedule: T - H: 9:30 – 10:45 AM • Classroom: TC - 206
• Credit: 3 • Enrollment: 26
Your fi nished JSF page CoursePage.jsp is shown in Figure 9.133 .
Click on the Update button to update this course record in the Course table in our sample database.
To test this course record updating action, there are more than one way can be used.
The fi rst way is to open the Course table to confi rm that this course has been updated.
But the second way, which is to select the course_id whose course details have been updated from the CourseList Listbox to get course details, is an easy way to confi rm this course data updating.
Now let ’ s use the second way to test this course data updating. Just click any other
course_id , such as CSE - 330, from the CourseList Listbox. Then click on the course_id CSC - 233B whose details have been updated, and click on the Details button to retrieve all course details. It can be found that this course is really updated based on the updating information shown in Figure 9.133 .
Generally, it is recommended to recover this updated course in the Course table in our sample database to keep our database neat and clean. However, we will keep this course right now since we need to use this record to perform the course deleting action in the following section.
Our Web client project to consume our Web service operation UpdateCourse() is successful!
Next, let ’ s discuss how to consume the Web service operation DeleteCourse() to delete an existing course record from our Course table for a given faculty member.
9.16.4.4 Build Codes for the Delete Button Method to Delete Courses
Now let ’ s do the coding for the Delete() method in our Java managed bean CourseBean.
java to call a Web service operation DeleteCourse() to delete an existing course record from our Course table for a given course_id .
Figure 9.133. The running result of updating a course CSC - 233B.
c09.indd 901
c09.indd 901 7/20/2011 11:12:55 AM7/20/2011 11:12:55 AM
www.traintelco.com
The function of this method is to delete a course record for a given course_id via the Web service operation DeleteCourse() as the Delete button in our JSF page
CoursePage.jsp is clicked by the user.
First, let ’ s add our Web service operation DeleteCourse() into this Delete() method by performing the following operations:
1. Open the code window of our Java managed bean CourseBean.java and browse to the
Delete() method.
2. Browse to the Web Service References node under our client project WebClientOracle . 3. Expand our Web service until all our fi ve Web service operations have been exposed.
4. Drag the DeleteCourse operation and place it inside the Delete() method.
5. A piece of codes is automatically created and added into this method.
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 course deleting requirements. Enter the codes that are shown in Figure 9.134 into this method, and the modifi ed codes have been highlighted in bold.
Let ’ s have a closer look at this piece of modifi ed codes to see how it works.
A. A local variable delete is created and this is a Boolean variable used to hold the running result of the execution of the Web service operation DeleteCourse() .
B. The Web operation DeleteCourse() is called to delete an existing course record via our Web service. The execution result that is a Boolean variable is returned and assigned to the local variable delete .
C. If a false is returned, which means that this course data deleting has failed, the system
println() method is used to indicate this situation.
D. The catch block is used to track and display any possible exception during this data delet- ing process.
E. Finally, a null is returned. Since we never use this returning value in this application, it is not important to us.
Figure 9.134. The modifi ed codes for the Delete() method.
A
B C D
E
public Boolean Delete() { boolean delete = false;
try { // Call Web Service Operation
org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();
delete = port.deleteCourse(selectedItem);
if (!delete)
System.out.println("Error in DeleteCourse()...");
} catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
} return null;
}