Second Thoughts about Software, Technology and Business...

Wednesday, March 19, 2008

Web Services using NetBeans 5.5 [2]

In the previous article we have created and deployed successfully a working Web service. Now we are going to build the 3 Clients that will consume this web service.

9.From the context menu of your web project select to create a new "Web Service Client". This is how the things goes in NetBeans, whatever the type of your project was to let it consume a web service you must create a web service client in this project and this is logical indeed as you must provide some information (mainly the WSDL Document path) to the client.

10. In the "New Web Service Client" dialog box, browse to your projects list to select the "sayHelloWS" web service and NetBeans will automatically figure out the WSDL path of the service. Also create a new java package for the test client - here i called it com.ws.client

11. After finishing you can see the WSDL Document retrieved for our "sayHelloWS" web service.

12. Now we would like to invoke directly the "sayHello" method in the service. Open the "index.jsp" of your web project and between the body tags right click then choose "Web Service Client Resource" then "Call Web Service Operation". As we can see NetBeans makes the whole process of developing and consuming web services so simple and straight forward.

13. Choose the only method available "sayHello". As you can see in the figure below the code was automatically generated to call the method.

The code generated is self explanatory. At the first few lines we create a new reference for the web service then obtain the port associated with it then we simply pass the correct type parameter and obtain the result then print it.
I have highlighted the name passed to the method "Java Duke" for test purpose.

14. Run the whole web project and you should see the following result.

As you can see the response of the service is printed on the screen with the correct input. Congratulations!

15. Using the Same logic we can also create a J2SE JFrame Based web service client and a J2ME MIDP 2.0 Mobile client as well. The screen shots for those 2 projects invoking the same "sayHelloWS" web service are listed below.



Well, as we saw here we could create 3 Clients invoking a backend web service in just 15 steps. Although the example presented here maybe simple but it is rather a good start for developing Web Services based applications.

Hope you liked this short tutorial :)

No comments: