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

Practical Database Programming With Java

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

898 Chapter 9 Developing Java Web Services to Access Databases

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 field in our JSF page CoursePage.jsp, each piece of detailed course information will be displayed in each of those inputText fields. The first 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 finished 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 fine, 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 field. 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 five inputText fields, 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.

9.16 Build a Web-Based Web Client Project to Consume the Web Service 899

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 five 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 modifications 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 modified codes have been highlighted in bold.

public Boolean Update() {

Aboolean update = false; ArrayList al = new ArrayList();

Bal.clear();

Cal.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();

Dupdate = port.updateCourse(al);

Eif (!update)

System.out.println("Error in UpdateCourse()..."); F } catch (Exception ex) {

msgDlg.setMessage("exception is: " + ex); msgDlg.setVisible(true);

}

G return null;

}

Figure 9.132. The modified codes for the Update() method.

900 Chapter 9 Developing Java Web Services to Access Databases

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

A.Two local variables, update and al, are created first. The first 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 first 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 identical 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 updating 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 finished 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 fine, 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 field. Then click the Select button to query all course_id taught by this selected faculty member. Immediately, you can find 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 five pieces of updated course information shown below into five text fields.

• Course:

Network Theory

Schedule: T-H: 9:30–10:45 AM

Classroom: TC-206

Credit:

3

Enrollment: 26

9.16 Build a Web-Based Web Client Project to Consume the Web Service 901

Figure 9.133. The running result of updating a course CSC-233B.

Your finished 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 first way is to open the Course table to confirm 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 confirm 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.

902 Chapter 9 Developing Java Web Services to Access Databases

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 five 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 modifications 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 modified codes have been highlighted in bold.

Let’s have a closer look at this piece of modified 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 deleting process.

E.Finally, a null is returned. Since we never use this returning value in this application, it is not important to us.

public Boolean Delete() {

Aboolean delete = false;

try { // Call Web Service Operation

org.ws.oracle.WebServiceOracle port = service.getWebServiceOraclePort();

Bdelete = port.deleteCourse(selectedItem);

Cif (!delete)

System.out.println("Error in DeleteCourse()..."); D } catch (Exception ex) {

msgDlg.setMessage("exception is: " + ex); msgDlg.setVisible(true);

}

E return null;

}

Figure 9.134. The modified codes for the Delete() method.

9.16 Build a Web-Based Web Client Project to Consume the Web Service 903

Now we have finished all coding process for the course data deleting 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 fine, 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, enter a desired faculty name, such as Jenney King, into the Faculty Name field. Then click the Select button to query all course_id taught by this selected faculty member. Immediately, you can find 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 delete the course CSC-233B, keep this course_id selected from the CourseList Listbox, and click on the Delete button.

To confirm this course deleting action, two ways can be utilized. First, you can open our Course table to check whether this course has been deleted from our database. Another way, which is easy, is to use the Select button to try to retrieve this deleted course from our database.To do that, just keep the selected faculty member Jenney King in the Faculty Name combo box unchanged and click on the Select button. It can be found from the returned courses, exactly all course_id taught by the selected faculty, that no CSC-233B existed, as shown in Figure 9.135.

Our course record deleting using Web service is successful.

It is highly recommended to recover this deleted record from our database to keep our sample database neat and clean. You can recover this course using either the Insert Record method in the opened Course table via Services window in NetBeans IDE or Oracle Database 10 g XE Object Browser. Refer to the data shown in Table 9.13 to recovery this course CSC-233B.

Figure 9.135. The running result of deleting a course CSC-233B.

904 Chapter 9 Developing Java Web Services to Access Databases

Table 9.13. The original course record CSC-233B in the Course table

course_id

course

 

credit

classroom

schedule

 

enrollment

faculty_id

 

 

CSC-233B

Introduction to Algorithms

3

 

TC-302

M-W-F: 11:00-11:55 AM

19

 

K69880

At this point, we have finished all developing and building processes to consume our Web service using a Web-based client project. A complete Web-based client project that is used to consume our Web service to query and manipulate data against our Oracle database, 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).

9.17 CHAPTER SUMMARY

A detailed discussion and analysis of the structure and components about Java Web Services are provided in this chapter. Two popular Java Web Services, REST-Based and SOAP-Based services, are discussed in detail with illustrations. The procedure of building a typical SOAP-Based Web service project is introduced with a real project example.

Starting from Section 9.5, two typical SOAP-Based Web service projects, WebServiceSQL, which is used to access and manipulate data against a SQL Server 2008 database, and WebServiceOracle, which is used to access and manipulate data against an Oracle database, are discussed and analyzed in detail with two real project examples.

To consume these two kinds of Web services, four real client projects are developed and built with detailed coding processes and illustrations:

WinClientSQL: a Windows-Based Web client project to consume the Web service WebServiceSQL to perform data query and manipulations to the Faculty table in our sample SQL Server 2008 database.

WebClientSQL:aWeb-BasedWebclientprojecttoconsumetheWebserviceWebServiceSQL to perform data query and manipulations to the Faculty table in our sample SQL Server 2008 database.

WinClientOracle: a Windows-Based Web client project to consume the Web service WebServiceOracle to perform data query and manipulations to the Course table in our sample Oracle database.

WebClientOracle: a Web-Based Web client project to consume the Web service WebServiceOracle to perform data query and manipulations to the Course table in our sample Oracle database.

A console-based testing project OracleTest to test the data insertion and updating actions against our sample Oracle database is also included in this chapter.

All of these real projects have been tested and debugged, and can be used without modifications. To use these project examples, one needs to install

Glassfish v3 Web application server

Microsoft SQL Server 2008 Express database and management studio

Microsoft SQL Server JDBC Driver

Homework 905

Oracle Database 10g XE

Oracle JDBC Driver

All of these software tools and drivers can be downloaded and installed on the users’ computer with free of charge. Refer to Appendices to finish these downloading and installation processes.

HOMEWORK

I. True/False Selections

_____1. Unlike Java Web applications, the Java Web Services provide an automatic way to search, identify, and return the desired information required by the user through a set of methods installed in the Web server.

_____2. Java Web Services provide graphic user interfaces (GUIs) to enable users to access the Web services via the Internet.

_____3. Web services can be considered as a set of methods installed in a Web server, and can be called by computer programs installed on the clients through the Internet.

_____4. Two popular Java Web Services are: REST-based and SOAP-based services, and both are supported by NetBeans IDE.

_____5. Both Web service models, JAX-WS and JAX-RPC, are popular and updated models used in Web service developments.

_____6. Compared with REST-based service, SOAP-based Web Services are more suitable for heavyweight applications using complicated operations and for applications requiring sophisticated security and reliability.

_____7. Unlike ASP.NET Web Services, a Java SOAP-based Web service project is involved in a Java Web application project in which the Web service can be deployed based on an appropriate container.

_____8. To access a Web service, one does not have to call any operation defined in the Web service.

_____9. Before one can call a Web service operation, a Web service reference must have been established for the client project.

____10. It is unnecessary to update a Web service each time when consuming it in a client project; however one must deploy that Web service each time when start it from NetBeans IDE.

II. Multiple Choices

1.In a SOAP-Based Java Web Service, the SOAP means ___________.

a.Statement Object Access Protocol

b.Simplified Object Access Protocol

c.Simple Object Access Protocol

d.Structure Object Access Protocol

2.In a REST-Based Java Web Service, the REST means ___________.

a.REpresentational State Transfer

b.REpresentational State Transmitter

c.REpresentational Status Transfer

d.Rapid Essential State Transfer

906 Chapter 9 Developing Java Web Services to Access Databases

3.When using a REST-Based Web service, only four methods are available: ___________.

a.INPUT, OUTPUT, POST, and DELETE

b.SAVE, PUT, POST, and DELETE

c.GET, EXECUTE, POST, and DELETE

d.GET, PUT, POST, and DELETE

4.The protocol used in the REST-Based Web Services is ___________.

a.FTP

b.XML

c.HTTP

d.TCP/IP

5.To effectively find, identify and return the target information required by computer programs,

aSOAP-based Web Service needs the following components, ___________.

a.XML and WSDL

b.SOAP, UDDI and WSDL

c.UDDI, XML and SOAP

d.WSDL, XML, UDDI and SOAP

6.SOAP is a simple ___________-based protocol to help applications developed in different platforms and languages to exchange information over ___________.

a.HTML, HTTP

b.XML, HTTP

c.FTP, TCP/IP

d.XML, Internet

7.In WSDL terminology, each Web service is defined as a ___________, and each Web method is defined as an abstract ___________.

a.Method, function

b.Service, operation

c.End point, function

d.Port, operation

8.SOAP is used to wrap and pack the data tagged in the ___________ format into the messages represented in the ___________ protocol.

a.XML, SOAP

b.HTML, HTTP

c.FTP, TCP/IP

d.SOAP, XML

9.When building a Java Web service, a ___________ that contains a Web container for the

___________ must be built first.

a.Web service, Web application

b.Web client, Web consuming project

c.Web service, Web client project

d.Web application, Web service

10.To consume a Web service, a ___________ must be established in the client project.

a.Web service reference

b.Web service operation

 

 

Homework 907

Web Server

SQLServer

Win-Based

 

Database

Client

 

Server

 

Java Session

JAPI &

Web Services

Entity

Beans

 

Classes

 

 

Web-Based

Client

Figure 9.136. The structure of building a new Web service project.

c.Web service directory

d.All of them

III. Exercises

1.Provide a brief description about the advantages of using a SOAP-based Web service.

2.Illustrate the structure and components of SOAP-based Web services.

3.Provide a brief description about procedures of building a typical SOAP-based Web service project.

4.Provides a brief description about how to establish a Web service reference for a given client project to enable the latter to consume that Web service.

5.Explain the operational sequence of adding a Web service operation into a method in a client project to enable the latter to call that operation.

6.Using the structure shown in Figure 9.136 to build a Web service project WSSQLBean and replace the Java runtime object with the Java managed bean to perform data actions against the Faculty table in our SQL Server 2008 database.

7.Develop a similar Web service to access and manipulate data against the Student table in our sample SQL Server 2008 database.

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