Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Practical Database Programming With Java

.pdf
Скачиваний:
778
Добавлен:
10.06.2015
Размер:
31.58 Mб
Скачать

778 Chapter 9 Developing Java Web Services to Access Databases

• Click on the Design button on the top of this window.

To open this dialog using the Web service context menu,

Find our target Web service output file WSTest from the Web Services node in the

Projects window.

Right-click on that node to open the context menu.

Click on Add Operation menu item in either the visual designer or the context menu. A dialog box appears where you can define the new operation.

Perform the following operations to add a new addition operation or new method:

1. In the upper part

of the Add

Operation dialog box, type Add into the Name field

and type int into

the Return

Type drop-down list. In the lower part of the Add

Operation dialog box, click on the Add button and create a parameter of type int named input1. Then click on the Add button again and create the second parameter of type int called input2. Your finished Add Operation dialog should match the one that is shown in Figure 9.6.

2.Click on the OK button to close this dialog. The newly added operation is displayed in the visual designer, as shown in Figure 9.7.

3.Click on the Source button on the top of this window to open the code window of this Web service main body file, and you can find that our new Web operation or method Add() has been added into this class, as shown in Figure 9.8.

4.In the opened WebMethod Add(), enter the codes shown below into this method:

int result = input1 + input2; return result;

5.Your finished codes for this new operation method, which have been highlighted, are shown in Figure 9.9. The function of this operation is simple, and it only adds two input numbers entered by users via a client and returns the result to the client.

Figure 9.6. The finished Add Operation dialog.

9.3 The Procedure of Building a Typical SOAP-Based Web Service Project 779

Figure 9.7. The opened visual designer.

Figure 9.8. The codes created for the new added operation.

780 Chapter 9 Developing Java Web Services to Access Databases

Figure 9.9. The finished codes for the new operation method.

At this point, we have finished developing our Web service project, and next, we need to deploy it to the selected Web container and test it with some consuming projects.

9.3.4 Deploy and Test the Web Service on the Selected Container

The NetBeans IDE provides a server’s test client to assist us to test our Web service after it has been successfully deployed. Perform the following operations to deploy our Web service to our Web container GlassFish v3:

1.Right-click on our project WSTestApplication from the Projects window and choose the Deploy item. The NetBeans IDE will start the application server, build the application, and deploy the application to the server. You can follow the progress of these operations in the WSTestApplication (run-deploy) and the GlassFish v3 server in the Output window view.

2.Enter admin and reback as the username and password to the Java Glassfish v3 Server to login and start this Web server. Recall that we used these login data when we installed the Java Glassfish v3 Server in Section 5.3.5.2.1 in Chapter 5. Refer to that section to get more details for these login data.

3.If everything is fine, a successful deploy result should be obtained and displayed in the Output window, as shown in Figure 9.10.

To test our Web service, perform the following operations:

1.In the opened Projects window, expand the Web Services node of our project ,and rightclick on our target Web service output file WSTest, and choose the Test Web Service item.

9.3 The Procedure of Building a Typical SOAP-Based Web Service Project 781

Figure 9.10. The deployment result.

Figure 9.11. The Web service testing page.

2.The NetBeans IDE will display a tester page in your browser if everything is fine, which is shown in Figure 9.11.

To test our Web service project, enter 5 and 3 to the two input boxes, and click on the add button. You can find that a successful running result of our Web service is displayed with an addition result of 8, which is shown in Figure 9.12.

782 Chapter 9 Developing Java Web Services to Access Databases

Figure 9.12. The Web service testing result.

One point to be noted is that if you are using the Tomcat Web Server as your application server, you would not find this tester page, and only a testing successful page is displayed without the page testing ability available. Also, if you are deploying a Web service built with EJB module, you cannot find this tester page either, since the NetBeans IDE will not support this testing function to any EJB module.

Next, let’s build a Web service consuming project to consume our Web service.

9.3.5 Create Web Service Clients to Consume the Web Service

In fact, you can develop any kind of Java applications as a consuming project to consume a Web service, such as a general desktop Java application project, a Java servlet, or a JSP page in a Web application.

To make this client project simple, we prefer to build a simple Java desktop application project WSTestClient to consume this Web service.

Perform the following operations to create our client project:

9.3 The Procedure of Building a Typical SOAP-Based Web Service Project 783

1. Choose File > New Project menu item to open New Project wizard. Select Java from the Categories list and Java Application from the Projects list, and click on the Next button.

2.Name the project WSTestClient and select an appropriate location for this client project. Leave Create Main Class checkbox checked and accept all other default settings. Your finished Name and Location wizard should match the one that is shown in Figure 9.13. Click on the Finish button to complete this new project creation process.

3.Right click on our new client project WSTestClient node from the Projects window and choose New > Web Service Client item to open the New Web Service Client wizard.

4.Click on the Browse button that is next to the Project radio button to browse to our Web service project WSTest, as shown in Figure 9.14. Click on our Web service WSTest and click on the OK button.

Figure 9.13. The finished Name and Location wizard.

Figure 9.14. The Web service browse wizard.

784 Chapter 9 Developing Java Web Services to Access Databases

Figure 9.15. The finished New Web Service Client wizard.

Figure 9.16. The new added Web Service References node and components.

5. Your finished New Web Service Client wizard should match the one that is shown in Figure 9.15. Click on the Finish button to complete this new consuming project creation process.

6.A new node named Web Service References with the following components has been added into our client project WSTestClient, as shown in Figure 9.16:

A.Our target Web service output file WSTest

B.Our Web service class file WSTestService

9.3 The Procedure of Building a Typical SOAP-Based Web Service Project 785

public class Main {

/**

* @param args the command line arguments */

public static void main(String[] args) { // TODO code application logic here

Atry {

Borg.wstest.WSTestService sevice = new org.wstest.WSTestService();

Corg.wstest.WSTest port = sevice.getWSTestPort();

Dint a = 5, b = 7;

Eint result = port.add(a, b);

FSystem.out.println("Result = "+result);

}

G catch (Exception ex){ System.out.println("exception" + ex);

}

}

}

Figure 9.17. The codes for the main() method.

C.Our Web service port file WSTestPort

D.Our operation Add() method

Now let’s build the codes for this consuming project to consume our Web service. Perform the following operations to build the codes for this consuming project:

1.Double click on our main class file Main.java that is located at the Source Packages\ wstestclient node to open the code window of this file.

2.Enter the codes that shown in Figure 9.17 into the main() method on this file.

Let’s have a closer look at this piece of codes to see how it works.

A.A try . . . catch block is used to call our Web service to perform a two-integer addition operation.

B.A new Web service instance service is created based on our Web service class

WSTestService.

C.The getWSTestPort() method is executed to get the current port used by our Web service. This port is returned and assigned to a new Port instance port.

D.Two testing integers are created and initialized with 5 and 7, respectively.

E.The operation method Add() in our Web service is called to perform this addition operation. The running result of this method is assigned to a local variable named result.

F.The result is displayed on the Output window.

G.Any exception during this Web service calling process will be tracked and displayed on the Output window, too.

Now let’s build and run our client project to call the Add() method built in our Web service to perform this two-integer addition operation.

786 Chapter 9 Developing Java Web Services to Access Databases

Figure 9.18. The running result of calling our Web service.

Click on the Clean and Build Main Project button to build our client project. Then right click on our project WSTestClient and select the Run menu item from the pop-up menu. The running result is shown in Figure 9.18.

It can be found that the calling of our Web service is successful, and the addition result of 12 has been returned. Our first Web service project is successful.

At this point, we have gotten a fundamental knowledge and basic understanding about Java Web Services. Now let’s start building some real Java Web Services projects to perform database query and manipulation operations against our sample database.

9.4 GETTING STARTED WITH JAVA WEB SERVICES USING NETBEANS IDE

In the following sections, we will develop and build different Java Web Services projects based on two main database systems, SQL Server 2008 and Oracle Database 10 g XE, and different database operations.

Two kinds of real Java Web service projects will be developed and built:

1.Web services project to access and manipulate data against the SQL Server database.

2.Web services project to access and manipulate data against the Oracle database.

With the following data actions by adding the different operations or methods to those two Web service projects, respectively (because of the space limitations, we will concentrate on accessing and manipulating data against the Faculty table in our sample SQL Server 2008 database, and accessing and manipulating data against the Course table in our sample Oracle database):

1.Query data from the SQL Server 2008 database with QueryFaculty() operation.

2.Insert data into the SQL Server 2008 database with InsertFaculty() operation.

3.Update and delete data against the SQL Server database with UpdateFaculty() and

DeleteFaculty() operations.

4.Query data from the Oracle database with QueryCourse() operation.

5.Query detailed course information from the Oracle database with DetailCourse() operation.

9.5 Build Java Web Service Projects to Access SQL Server Database 787

6.Update and delete data against the Oracle database with UpdateCourse() and

DeleteCourse() operations.

For each Web services project, we need to build an associated Web client project to consume the Web services project we built to test its function.The following client projects will be built:

1.Web client project to consume the Web service to access the SQL Server database

2.Web client project to consume the Web service to insert data into the SQL Server database

3.Web client project to consume the Web service to update and delete data against the SQL Server database

4.Web client project to consume the Web service to access the Oracle database

5.Web client project to consume the Web service to query course details from the Oracle database

6.Web client project to consume the Web service to update and delete data against the Oracle database

As we know, we can develop any kind of client project to consume a Web service, either a standard Java desktop application, a JSP page or a JSF page. We will develop and build different client projects to consume our Web services to enable our projects to meet the actual needs in our real world.

Let’s start from the SQL Server database.

9.5 BUILD JAVA WEB SERVICE PROJECTS TO ACCESS SQL SERVER DATABASE

In this section, we will discuss how to access and perform queries and manipulations against SQL Server 2008 database using Java Web Services. To make our Web Services project simple, we will use the following components to fulfill this query and manipulation:

Build different operations or methods in our Web services as interfaces to communicate with Web clients that will be built in the future to perform desired data actions.

Use runtime object method to actually access and query our sample SQL Server 2008 database.

The structure and components used in our Web services are shown in Figure 9.19. Now let’s create our first Web service project WebServiceSQL to perform data query

and manipulation against our sample database.

9.5.1 Create a New Java Web Application Project

WebServiceSQLApp

When creating a new Web service application project, we need to select a desired container to deploy our Web service. Generally, we can either deploy our Web service in a

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]