C H A P T E R 5 |
Creating a Client for the Tutorial Application |
This chapter shows you how to run the DiningGuide application using a provided Swing client that communicates with the web service you created in .
The provided client contains two Swing classes, RestaurantTable and CustomerReviewTable. You add these classes to the WebService package, then execute the RestaurantTable class to run the application.
This client is very primitive, provided only to illustrate how to access the methods of the client you have generated for the web service.
This chapter covers these topics:
The client classes are provided as Java class files in the DiningGuide.zip file, which you can download from the Developer Resources portal.
To copy the two provided Java client classes into the DiningGuide application:
1. In the IDE, create the Client package within the DiningGuide folder.
a. Right-click the DiningGuide folder and choose New Java Package.
b. In the New wizard, type Client in the Name field and click Finish.
2. Unzip the DiningGuide.zip file from the Developer Resources portal.
a. Download the DiningGuide.zip file from the Developer Resources portal.
http://forte.sun.com/ffj/documentation/tutorialsandexamples.html
b. Unzip the file to a local directory, for example, the /MyZipFiles directory.
3. Using a file system command, copy the two client files from the DiningGuide source files to the Client package, as follows:
$ cp /MyZipFiles/DiningGuide/Client/*.java /DiningGuide/Client |
4. In the IDE's Explorer, expand the DiningGuide/Client package and verify that the two new classes are there.
Run the DiningGuide application by executing the RestaurantTable class, as follows:
1. In the IDE, click the Runtime tab of the Explorer.
2. Expand the Server Registry, the Installed Servers, the Sun ONE Application Server 7, and its subnodes.
3. Right-click the Deployed Applications subnode of the server instance.
4. Make sure the DiningGuideApp application is still deployed.
If it is still deployed, a DiningGuideApp node is displayed under the Deployed Applications subnode.
5. If it is not still deployed, deploy it, as described in .
6. In the Filesystems tab of the Explorer, right-click the RestaurantTable node and choose Execute.
The IDE switches to Runtime mode. A Restaurant node appears in the execution window. Then, the RestaurantTable window is displayed, as shown:
7. Select any restaurant in the table and Click the View Customer Comments button.
For example, select the French Lemon restaurant. The CustomerReviewTable window is displayed. If any comments exist in the database for this restaurant, they are displayed, as shown. Otherwise, an empty table is displayed.
8. Type a something in the Customer Name field and in the Review field and click the Submit Customer Review button.
The record is entered in the database and is displayed on the same CustomerReviewTable window, as shown:
9. Play around with the features, as described in User's View of the Tutorial Application.
10. Quit the application by closing any window.
After you quit the application, the execution window shows that the Sun ONE Application Server 7 process is still running. You need not stop the application server. If you redeploy any of the tutorial's J2EE applications or rerun the test clients (but not this Swing client), the server is automatically restarted.
When you quit the IDE, a dialog box is displayed for terminating any process that is still running (including the application server or the web server). Select each running process and click the End Tasks button. You can also manually terminate any process at any time while the IDE is running by right-clicking its node in the execution window and choosing Terminate Process.
The two client classes you have installed in the DiningGuide application are composed of Swing components and actions that were created in the Form Editor, and several methods that were created in the Source Editor. The methods added in the Source Editor include the crucial task of instantiating the client so that its methods become available to the client.
To help you understand how the Swing client interacts with the web service, the next few sections discuss the main actions of the client, namely:
Displaying restaurant data is accomplished by the RestaurantTable class's methods, which instantiate the client and call its getAllRestaurants method, as follows:
1. RestaurantTable.getAllRestaurants method instantiates the client, calls the client's getAllRestaurants method to fetch the restaurant data, and returns the fetched restaurant data as a vector.
2. The RestaurantTable constructor puts the returned restaurant data into the restaurantList variable and calls RestaurantTable.putDataToTable.
public RestaurantTable() { initComponents(); restaurantList=getAllRestaurants(); putDataToTable(); } |
3. The RestaurantTable.putDataToTable method iterates through the vector and populates the table.
4. The RestaurantTable.Main method displays the table as a Swing jTable component.
public static void main(String args[]) { new RestaurantTable().show(); } |
Displaying customer review data begins when the RestaurantTable's button component's action instantiates a CustomerReviewTable. The CustomerReviewTable's methods fetch the customer review data by means of the client's methods, and populate the table. The RestaurantTable's button component's action then displays it, as follows:
1. When the RestaurantTable's button is pressed to retrieve customer review data, the RestaurantTable.jButton1ActionPerformed method instantiates a new CustomerReviewTable object, calls its putDataToTable method, and passes it the data of the selected column.
2. The CustomerReviewTable.putDataToTable method calls the CustomerReviewTable.getCustomerReviewByName method, passing it the selected restaurant name, assigning the returned vector to the customerList variable.
3. The CustomerReviewTable.getCustomerReviewByName method instantiates a client (if required) and calls its getCustomerreviewsByRestaurant method, passing it the name of the selected restaurant.
4. The review data is passed up to the CustomerReviewTable.putDataToTable method, which iterates through it and populates the table.
5. The RestaurantTable.jButton1ActionPerformed method then displays the data.
When the user types a name and review comments on the Customer Review window and clicks the Submit Customer Review button, the CustomerReviewTable's jButton1ActionPerformed method creates the review record in the database by means of the client's methods, then refreshes the Customer Review window, as follows:
1. When the CustomerReviewTable's button is pressed to submit a customer review record, the CustomerReviewTable.jButton1ActionPerformed method instantiates a new client (if required) and calls its createCustomerreview method, passing it the restaurant name, the customer name, and the review data.
2. This same method (jButton1ActionPerformed) calls the CustomerReviewTable.refreshView method.
3. The CustomerReviewTable.refreshView method calls the putDataToTable method, passing it the restaurant name.
void refreshView() { try{ while(TableModel.getRowCount()>0) { TableModel.removeRow(0); } putDataToTable(RestaurantName); repaint(); } catch (Exception ex) { ex.printStackTrace(); } } |
4. The CustomerReviewTable.putDataToTable method populates the table.
5. Then the CustomerReviewTable.refreshView method repaints the window, showing the new data.
Copyright © 2003, Sun Microsystems, Inc. All rights reserved.