Java Web Services in NetBeans Part-II

The promise of web services, as said earlier, is interoperability between disparate systems such as JVM and Dot Net. For the present, we will see the facilities offered by NetBeans to create web service clients and  create Java and JSP clients. We will see a Dot Net client for the Java web services earlier created, at a future date. Why Java and JSP?  To show that client need not be a web application, though web services are published through a web server- the client can be either a web application or a SDK project. NetBeans6 is a Ruby IDE also. But there is no out of box support for Ruby web service in NetBeans6.0 similar to what is available for Java. William Brogden in his “SOA Tips” –“Ruby as Web Service client Language “ points out that using the SOAP tools of Ruby is a lot trickier than expected and the documentation on these tools are not well developed. As ours is only a small sample web service, we will follow his example and create a ruby client also He also points out( in his “Web Services with Ruby on Rails”) though SOAP support is available in Rails, Rails developers encourage REST approach and hence Rails does not implement more advanced WS-* features found in other frame-works.

Create a new Java project in NetBeans IDE, say calcClient . Right click on it in the Projects window. Choose   New -> Web Service client .You will get the wizard as in Fig-1.

Browse for your web service and choose Calculator/calculate. You will get wizard as in Fig-2

After you click Finish You may get some message Calculator has been modified from outside and  Do you want to reload it .Click No. If  you look at  calcClient in the Project window and expand the node Web Services References You can see Fig-3

Double click on an empty line in the main method and drag the node multiply to it. The IDE will generate the code found in Fig-4.

Fig-4

try { // Call Web Service Operation
calc.CalculateService service = new calc.CalculateService();
calc.Calculate port = service.getCalculatePort();
// TODO initialize WS operation arguments here
int x = 0;
int y = 0;
// TODO process result here
int result = port.multiply(x, y);
System.out.println(“Result = “+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
// TODO code application logic here
}

Note: Alternatively, instead of dragging the multiply  node, you can right-click in the editor and then choose Web Service Client Resources > Call Web Service Operation.

Modify the code adding some values for x and y in place of 0,say 5 and 8

In the Projects window Right Click on calcClient and choose Run

You can see the result in the Output window as shown in Fig-5

Now we will see how to create a JSP client consuming the above web services . Close the calcClient Project. Create a  new Web  Project  say calcJspClient.

Create a Web Service Client following the steps narrated earlier. In the wizard as in Fig-6 Browse to your web service and give the package name as calc.

When You click finish the IDE will generate code.

Delete what is found in body of index.jsp and drag mutiply node seen in  Fig-7 from Web Services Reference Folder in calcJspClient into the empty space created from the above deletion.

The above figure also shows the generated code also. The code generated is separately given in Fig-8 for clarity.

Fig-8

<%-- start web service invocation --%><hr/>
<%
try {
calc.CalculateService service = new calc.CalculateService();
calc.Calculate port = service.getCalculatePort();
// TODO initialize WS operation arguments here
int x = 0;
int y = 0;
// TODO process result here
int result = port.multiply(x, y);
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%><hr/>

Give some value for x and y say 5 and 8 .Save and Run the calcJspClient Project by right clicking on it in the project window.
You can see the result in browser as in Fig-9

If you want a Servlet client  also ,you can refer to the article “Getting Stated with JAX-WS web services” available on Netbeans.org under Tutorials section.

Now will see how to create a Ruby client. Create a new project going File -> New Project and choosing Ruby -> Ruby Application and in the wizard that appears give values for Project Name and accept default values for others as shown Fig10.You can replace main.rb as shown in Fig-11.

In first line We  load the "soap/wsdlDriver" code from the standard library and in the second  create a variable containing a URL for accessing the WSDL file describing the calculate  service. Note that in Ruby, the variable named "wsdl_url" does not have an associated class type, it could refer to any object type.

In the third line a lot of behind-the-scenes work is performed and it ends up creating an instance of a class customized for the Web service described by the WSDL. The resulting object has been created with methods whose names are derived from the entries in the WSDL plus variables such as "wiredump_file_base" which gets set in the fourth line.  If this variable is set, the full text of the SOAP request and response will be written to local files, a big help in dealing with a complex Web service.
Line 5 creates a "hash" -- an array which associates values to the parameters to be passed to the method call.  The result is got and printed in the other lines.

After saving the file if you Run the Project you can see that new files are created -.soap_result_multiply_ request.xml,  soap_result_multiply_response.xml. In the navigator , if  you expand and see the response .xml,  you can see  the result of the multiply operation of calculate web service received by the ruby client program, though the output window shows only the object id.

 

Fig-11

require 'soap/wsdlDriver'

 wsdl_url = "http://localhost:8084/Calculator/calculate?WSDL"

 soap = SOAP::WSDLDriverFactory.new( wsdl_url ).create_rpc_driver

 soap.wiredump_file_base = "soapresult"

param ={"x" =>5,
"y" => 8
}
result = soap.multiply( param )

 puts result.to_s

 








}