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 Chapter 4.
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 proxy you have generated for the web service.
This chapter covers these topics:
The client classes are provided in two forms: source code and Java class files.
If you are using a Microsoft Windows system, you can create two classes in the IDE and replace their default code with the code you copy and paste from Appendix A. Code from these classes instantiates the client proxy, which is assumed to be in the same package. Therefore, create the client classes within the WebService package.
If you are using Solaris or Linux environments, copying code from a PDF file into the source editor results in an unformatted single line of code, which is rather inconvenient to read. Instead, use the class files provided in the DiningGuide2.zip file, which you can download from http://forte.sun.com/ffj/documentation/tutorialsandexamples.html
To create the two classes using by copying the source in Appendix A:
1. In the Explorer, right-click the WebService node and choose New Classes
Class.
2. Name the class RestaurantTable and click Finish.
The new RestaurantTable classes is created under the WebService package.
3. Repeat Step 1 and Step 2 to create the CustomerReviewTable class.
4. Open the classes in the Source Editor and delete all the default code from each class.
5. Copy all the code from RestaurantTable.java Source and the following three pages and paste it into the body of the RestaurantTable class.
6. Select all the pasted code in the Source Editor and press Control-Shift F.
This action reformats all the code correctly.
7. Repeat Step 5, copying all the code from CustomerReviewTable.java Source and the following three pages into the CustomerReviewTable class body.
8. Repeat Step 6 to format the pasted code properly.
9. Right-click the CustomerReviewTable node and choose Compile.
The CustomerReviewTable class should compile without errors.
10. Right-click the RestaurantTable class node and choose Compile.
The RestaurantTable class should compile without errors.
If you examine the code in the RestaurantTable and CustomerReviewTable classes, there are several comments warning against modifying some sections. These sections are Swing component code created in the Form Editor and should not be modified in the Source Editor. Normally, such restricted code has a blue background. If you restart the IDE, the source for this file will have a blue background for the restricted areas, and you will not be able to edit the code in those sections.
To copy the two provided Java client classes into the WebService package:
1. Unzip the DiningGuide2.zip file from the Developer Resources portal.
The portal is at http://forte.sun.com/ffj/documentation/tutorialsandexamples.html
For example, unzip the file to the /MyZipFiles directory.
2. Using a file system command, copy the two client files from the DiningGuide2 source files to the WebServices package.
$ cp /MyZipFiles/DiningGuide2/WebService/*.java /DiningGuide2/WebService |
3. In the IDE's Explorer, expand the DiningGuide2/WebService package and observe the two new classes.
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 Deploying the Test Application.
6. Right-click the server instance and choose Status to verify that the instance is running.
If the Stop Server Instance button is activated, the server is running. If it is not activated, click the Start Server Instance button to start it.
7. Click the Filesystems tab of the Explorer.
8. 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:
9. 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.
10. 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:
11. Play around with the features, as described in User's View of the Tutorial Application.
12. Quit the application by closing any window.
After you quit the application, the execution window shows that the Sun ONE Application Server 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 Tomcat 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 proxy 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 proxy and call its getAllRestaurants method, as follows:
1. RestaurantTable.getAllRestaurants method instantiates the client proxy, calls the client proxy'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 proxy'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 proxy (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 proxy'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 proxy (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 © 2002, Sun Microsystems, Inc. All rights reserved.