Here are the steps for creating a simple service which allows two numbers to be multiplied:
- create a new web application project called MultiplyApp (File/New project/Java Web/Web Application)
- Click on the MultiplyApp node in the project and add a new web service called MultiplyWS (new/Web Service and type MultiplyWS as the Web Service Name and raj as the Package)
- Expand the new created Web Services folder and look at the source of MultiplyWS. The default skeleton code should be showing error on MultiplyWS class as it has no operation as yet.
- Right click within the class definition and define the multiply operation with long return type and two int type parameters i and j (insert Code/Add Web Service Operation)
- Change the MultiplyWS. java code from return 0; to return i * j;
- Right click on the MultiplyApp node and select Deploy to deploy the web service.
- Right click on MultiplyWS in Web Service folder and select Test Web Service to see the service behaviour.
- Right click on MultiplyApp and select Properties and then choose Run category and change the relative path to /MultiplyWSService?Tester
At this stage our service is ready but we need service consumers which can be any Java application, a servlet or a JSP. We cover the steps for the Java application in some detail but the process for creating a JSP or servlet consumer follows the same pattern.
- Create a new Java application project called MultiplyClient (File/New Project/Java/Java Application). Leave the Create Main Class box checked while creating.
- Right click on the project name MultiplyClient and create new web service client (New/Web Service Client). Select the MultiplyWS service in Project by using Browse.
- Right click within main method of our Main.java class and select Insert Code/Call Web Service Operation and choose multiply from the available web service references.
- Set the values of i and j and add in a code to print exception if you want.
- Run the project and verify that the console printed the correct multiplication of i and j variables which we set in the previous step.
The process for creating the JSP or servlet-based client is the same, barring the fact that when we create a new project we choose File/New Project/Java Web/Web Application. We go through all the steps as above but we add the logic in a new servlet or a JSP and set the relative path in the project/properties/run accordingly to make these the entry points. Obviously the syntactic changes would be there to reflect that we are dealing with a servlet or a JSP when Insert Code/Call Web Service Operation happens. The only point worth mentioning is that the multiply operation can be dragged from the Web Service References to the appropriate point in the code to auto-generate the appropriate logic for the service call at that point. Thus for JSP we get this:
Experiment and enjoy!
Thats great i learnt a lot more from this one
ReplyDeleteThanks Dear
Thanks for your kind words. It makes the effort worthwhile.
ReplyDeleteGreat article, Rajeev..kudos..
ReplyDeleteasf
ReplyDeleteThanks
ReplyDeleteSpeed Infotech