In this section, you will create the Reminder Web service as explained in the case study. The Web service requires use of a database. The following sections discuss the SQL Server database structure. However, because we are developing the application in Java and will be using the JDBC-ODBC bridge to access the database, you can also use any other type of database.
The ReminderService Database
The ReminderService database contains the following tables:
Structure of the Users Table
Structure of the Occasions Table
Structure of the Reminders Table
Next, you will look at the stored procedures created for the database. Here is the code for the AddNewUser stored procedure:
Next, create the AddReminder stored procedure that allows a user to store the reminder for any occasion:
Last, the GetReminders stored procedure that allows a user to view his reminders is created, as shown in the following code:
Next, you will create the Java class that forms the basis of the Reminder Web service.
Creating the Java Class
The Java class, whose functionality will be exposed as a Web service, will call the stored procedures to access the data in the ReminderService database. Before you construct the Java classes, you need to configure the ODBC data source to establish backend connectivity in the class.
Configuring the ODBC Data Source
The Java class will make use of the JDBC-ODBC bridge to connect to the database. This implies that you have to configure an ODBC database, which you can accomplish as follows:
ODBC Data Source Administrator Window
The dialog box shows a list of data sources that are already configured. To create a new data source, click on the Add button. This brings up the Create New Data Source dialog box, which is shown in Figure
Create New Data Source Dialog Box
Create a New Data Source to SQL Server Dialog Box
Configuring the Authentication Options
This page allows you to select the appropriate authentication type. Select the SQL Server authentication, provide the username and password, and click on the Next button. This brings up the page shown in Figure.
Selecting the Default Database
Testing the Data Source Connection
Creating the ReminderService Class
You will now create the ReminderService class. This class will contain three public methods:
Note?/td>As you can see, this list is not conclusive. You will need more methods, for example, to modify or delete reminders, which you can develop later.
Each of the preceding methods reads or writes from the database. We will now introduce you to using JDBC to connect to a data source, which further connects to the database, and then reads and writes data from it.
First, you will have to connect to the ODBC data source that is configured and make use of it from within your Java class. To do so, you first have to load the required driver by using the following code:
Next, you must retrieve the connection with the database. To do so, use the following code:
You will be using Callable and Prepared statements in this class to access the database. Callable statements allow you to call stored procedures from JDBC applications.
A CallableStatement object is created using the prepareCall() method in the Connection class, and the general syntax of the method is as shown next:
In the preceding syntax, ? represents the parameters and the return values to be passed and retrieved from the method.
The following is a section of the ReminderService Java class that shows the AddReminder stored procedure being called using a CallableStatement:
To create a PreparedStatement, you would use the prepareStatement() method of the Connection class. The PreparedStatement() method takes an SQL string and passes it to the underlying database. The database verifies the syntax and does the query plan optimization but does not run the command. Instead, it returns a handle to the JDBC driver, which is then stored in a PreparedStatement object. You can use this object to execute the SQL command. Prepared statements are efficient. The repared Statement class has three methods: executeQuery(), executeUpdate(), and execute(), none of which take parameters.
Here is an example of a prepared statement being used in the Java class that is created for the Reminder service:
Here is the code for the complete Java class:
In the preceding code, notice that the main() method is marked as a comment block. You can use this method to test the functionality of the class before you create the Web service from it.
Creating and Deploying the Web Service
You are going to use the JDeveloper's Web Service Publishing Wizard to create a J2EE Web service from the Java class you created earlier. To run the JDeveloper for Oracle9i, run <JDeveloper_Home>JdevBinjdevw.exe. The user interface of JDeveloper is shown in Figure.
The JDeveloper IDE
Connecting to Oracle9i Containers for J2EE
You need to run the Oracle Containers for J2EE server. To do this, from your <OC4J_HomeJ2EEHome> folder, where you have installed it, execute the following command from the command line:java -jar oc4j.jar
Next, you need to set up a connection to the Oracle9i containers, which enables you to deploy your Web service to the application server. To do this, perform the following steps:
Creating a New Application Server Connection.
The Connection Wizard is shown in Figure.
The Connection Wizard
Naming the Connection
Authentication Information Page
This requires the authentication information to connect to OC4J. Enter the username as Admin and the password you would have given while setting up OC4J.
Connection Page in the Connection Wizard
Enter the data for the local machine name, the target Web site, and the path for the local directory where the admin.jar file is located.
Testing the Connection
Now that you have an OC4J connection, you can use the JDeveloper's Web Service Publishing Wizard to create your J2EE Web service.
Using JDeveloper to Create a J2EE Web Service
Perform the following steps to create a J2EE Web service:
Note?/td>Just to keep all files together, you might want to copy the Java file to the workspace project folder that will be created under the < Jdev_Home > jdev mywork folder.
New Project Dialog Box
Web Service Publishing Wizard
Step 1 Page of the Web Service Publishing Wizard
The Class Browser Dialog Box
Step 2 Page of the Web Service Publishing Wizard
Step 3 Page of the Web Service Publishing Wizard
The WSDL document contains a description of the Web service. Various tools use the WSDL document to create Web service clients. The content of the WSDL document created by the wizard is explained next:
Because the target namespace was not changed, the namespace remains as http://tempuri.org:
One of the methods exposed by the Web service, getReminders(), returns an array of strings. Here is the complexType declaration for that:
The Web service defines three operations: addUser, addReminder, and getReminder. Each of these operations uses two messages. For example, the addUser operation has the corresponding Request and Response message declarations shown next:
The port type and the operation declarations are shown next. As mentioned earlier, each of the operations uses a Request and a Response message defined earlier:
Note?/td>Notice the value of the location attribute of the soap:address tag. This tells you the URL where the Web service can be accessed.
Next, look at the deployment file. This file contains a list of the files to be deployed, the connection to which the Web service will be deployed, and the name of the WAR file that will be generated.
The web.xml file, shown next, contains information on the Web service and the servlet deployment:
Last, look at the interface file that defines the interface of the Web service:
Now that you have used the Web Service Publishing Wizard to create the files needed for your Web service, you need to deploy the Web service.
To deploy the Web service, perform the following steps:
Next, you will create a Java application that will test this Web service.
Testing the Web Service
This section shows you how to test your new J2EE Web service by creating an application that invokes one of the methods exposed by the Web service. The first client application will be created by using the JDeveloper for Oracle9i. Next, we will use Cape Clear's CapeStudio 3.0 to create test client applications in Visual Basic 6.0 and Java.
Creating the Client Application by Using JDeveloper
First, you need to create a Web service stub. You can then add this stub to a new project or to the existing project.
To do so, perform the following steps:
Creating a Stub/Skeleton for a Web Service
The Web Service Stub/Skeleton Wizard is displayed, as shown in Figure.
The Web Service Stub/Skeleton Wizard
The Select Web Service Description Page of the Stub/Skeleton Wizard
The Select Stubs/Skeletons to Generate Page of the Stub/Skeleton Wizard
Here is the content of the ReminderServiceStub.java file:
Note how neatly the methods in the stub encapsulate those in the Web service. Each method accepts the parameters from a client application and then makes a call to the Web service by using HTTP. Next, the method passes the parameters to the appropriate methods of the Web service.
Finally, you will create an application that will invoke the Web service. To do so, perform the following steps:
The New Dialog Box
Run the application and check the returned values.
Creating Client Applications by Using CapeStudio
Cape Clear offers an easy-to-use set of tools called CapeStudio for creating and deploying Web services. You also can use CapeStudio to create client applications for Web services created using other tools.
In this section, you will use a tool called WSDL Assistant, which is part of CapeStudio, to create client applications for the Java Web service you created earlier.
Creating a Visual Basic 6.0 Client
To generate a Visual Basic 6.0 client for the Web service you created earlier, you will use the WSDL Assistant tool that is part of the CapeStudio software. Figure shows the interface of the WSDL Assistant.
WSDL Client Tool.
In the text box labeled WSDL File, enter the path for the WSDL file of the ReminderService Web service. Specify an output directory where the files for the client can be generated. In the Generate section of the page, select the Client option button. Then, select the VB option button and enter the Project name. Click on the Generate button.
The assistant will create a Visual Basic 6.0, Standard EXE type project in the folder you specified. Open the project, and you will find the definition for a class that is a proxy of the Web service.
A class called ReminderServicePortTypeClient is created. This class has a property called endpoint that is used to set and get the value for the Web service endpoint. The property is initialized to the endpoint in the WSDL:
The following is the implementation for the addUser() method in the class, which, in turn, calls the addUser() method of the ReminderService Web service. The generated code uses the Soap Toolkit methods to serialize the method call into a SOAP message and then calls the Web service.
Note?/td>You need to download the Microsoft SOAP Toolkit ver2.0 SP 2.0 and install it because the code generated for the Visual Basic client uses it.
This is a Standard EXE project. Therefore, to execute this application, you need to add a form or a main procedure.
Add a form to the project and set it as the startup object in the Project Properties dialog box. Add a button to the form and the code to invoke a Web service method, as shown next:
Here, the code invokes the getReminders() method and displays the first reminder it returns. Run the application and check the functioning of both the client and the Web service.
Creating a Java Client
Run the WSDL Assistant and follow the same steps as before. Select Java as the client type and generate the Web service client. The assistant creates the following files for you in the folder that you specified:
Following is the definition of the ReminderServiceBindingClient class:
Following is the definition of the ReminderServiceBindingClientImpl class:
Following is the definition of the ReminderServiceBindingClientFactory class:
Following is the client application. The client application calls the exposed methods of the Web service one after the other by passing dummy parameters and checks for an error:
You will also find two more folders created: classes and lib. The Classes folder will contain the compiled classes for all generated Java files. The Lib folder has all the compiled classes along with the required support files packaged into a .jar file.
Set the class path to include the xerces.jar, xalan.jar, common.jar, capeconnect.jar, soapdirect.jar, and wsdl4j.jar files in the Capeclear capestudio3.0 lib folder. Execute the ReminderServiceBindingClientMainline class to run the test Java client application and verify the functioning of the Web service.
XML Related Interview Questions
|Soap Tool Interview Questions||HTML Interview Questions|
|PHP Interview Questions||ASP.NET Interview Questions|
|PHP5 Interview Questions||Java Interview Questions|
|CSS Interview Questions||XSLT Interview Questions|
|Java XML Interview Questions||XMLHttpRequest (XHR) Interview Questions|
|ebXML Interview Questions||XML DOM Interview Questions|
|XML-RPC Interview Questions||XSD Interview Questions|
|Soap Web Services Interview Questions||XSL Interview Questions|
|Xml Publisher Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.