Thursday 2 January 2014

Simple Web service with Net beans 6.8 and above

Here is the procedure to create a simple web service to understand how it works for a beginner or for a college student to do a basic exercise on java webservices using NetBeans IDE. I have used Netbeans 7.4 to create this and tested the same with netbeans 6.8 to 7.4 ( april 2014).

Requirement:
1.Install netbeans 6.8 full package with included glassfish server or tomcat server.
Link: https://netbeans.org/downloads/
2. Basic Java programming skills and to know to work with Netbeans.


STEP BY STEP PROCEDURE(VERY SIMPLE):

1. Open Netbeans IDE and go to File -> New Project -> Java Web & Web Application.
2.Provide a Name for the project (say for SampleWebService) and give next to complete the wizard.
   (Either tomcat or glassfish server must be selected as default) and if not download, install and retry).
3.Right click the ProjectName(SampleWebService)->New->Java Package
4.Enter name for Package( say for SamplePackage) and click Finish to close the wizard.
5.Right click the Source Package under Project Tab and select New->Other.
6.A new window opens. In that, under Categories-> Web Services and under File Types->Web Service and click Next.



7.Enter WebServiceName(E.g Calculator) and PackageName (as SamplePackage) and click Finish.

Once these are done, You would see something like this .


8.From WebService under ProjectTab right click Addition(Calculator)->Add Operation.
9.Under Add Operation dialog box enter Name(E.g Addition) and Return Type( as int) or  from Browse Tab.
10.Under Parameters Tab click Add tab to add variables.
11.Select Name and Type for the variables.For e.g. to add numbers add variables a and b and their type as Integer.



12. Click Ok and close the window.
13.Add the necessary code in the java file for calculation of numbers.



14.Now right click the SampleWebService and Deploy.
Its done!!
15.Create a simple servlet "myservs" in separate java package (eg."servs") which you will be using the calculator webservices.

TESTING THE WEBSERVICE:

15.To test the webservice right click the Calculator(WebService) ->Test WebService.
16.Enter numbers for calculation to test the web service.
17. Once the test is passed, web service is ready. 



NOTE: You can create the web service alone in separate application also and use it in your current web application.

Reference to web service:
18. Now you need to create the Web Service client  i.e a reference to the originally created web service . You can access the service only through references.
19. Right click on the web application project(SampleWebService) -> New -> Other-> and under web services , click on web service client.
20.Under WSDL and Client Location, Choose the project button and browse for the web service you want to make use of (Eg.SampleWebService->Calculator)  and enter some different package name(eg.mypack) .Then click finish. The web service could be from same project or different project.
21. You will see a "Web Service References" node in project tree . Expand it to see the service( function ) you want to use in your web or POJO application.
22. Just drag and drop the the service "Addition"(RedColor) under CalculatorPort to the servlet window(eg. here its "myservs") right side. You do not need to type any code as you will see that a new module Addition added in servlet code area where the service is called using a port object.
23. All you need to do is - make a call to the function passing necessary parameter( two numbers in this example .eg. addition(1,2))  from your servlet code.


24. Create a JSP call to servlet (eg. a simple href link to servlet to test) .Now DEPLOY AND RUN THE APPLICATION. Its done.




Please share your difficulties or error in the comment.

NOTE TO EXPERTS: I have given simple way of using webservice in java netbeans for beginners. Kindly ignore if there is any technical mistake and please make a comment to let me know about it.