Home Webservices Netbeans Java Client webservice

Related Tutorials


 
 

Share on Google+Share on Google+

Java Client webservice

Advertisement
Here we will show you how you can develop a webservice that calculates the factorial.

Factorial Web Services Example

     

Here we will show you how you can develop a webservice that calculates the factorial. We will also write the code to call the webservice.

NetBeans provides the wizard to help the developer to create, deploy and test the webservices application very quickly.

In this project we will make a web service program for factorial calculation.
Then develop a Java Client file instead of  Servlet or Jsp
Take a new web project.Give it the name  Java-WebService

Java Client file

Select the Glassfish Server

Java Client file

Right Click on the project and select the New Web Service

Java Client file

Now give the Web Service Name as factorial

Java Client file

It will create a web service in design view
Click on the Add Operation

Java Client file

Now in the fig give the name as fact1
Return type as String

Give one parameter name x
Data type as int
Click on ok
This will create the Web Service code with the
name as factorial and method name as fact1
This will be having the annotation as
@Webservice ,@Webmethod and @Webparam

Java Client file

Java Client file

Now click on the source view
Here make some modification in the business logic for factorial calculation

Java Client file
package pack1;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService()
public class factorial {
int fact=1;
  @WebMethod(operationName = "fact1")
    public String fact1(@WebParam(name = "x") int x) {
  for (int y=1;y<=x;y++){
  fact=fact*y;
  }
  return "factorial of "+x +"  is "+fact;
  }
}

Now build it
Deploy it on the server
After deployment it can be tested
So Right Click on it .
Select the Test Web Service
It will run the web service on the server and displayed in the browser

Java Client file

Here give the value 4 in the text box and click on fact1
It will display the calculated factorial with the SOAP request and SOAP response

Java Client file

Java Client file

Now we will make a new project for the Client
Take a new Java Application  project
Java Client file

Give the name as Java-webservice-Client

Java Client file

Now Right Click on this client project
Select NewàWebService Client
In the Dialog box give the WSDL url.
Copy it from the browser  and paste here

Java Client file

Java Client file

Java Client file

It will generate the client environment variables in the project
Now take a new Java class named Client.java
Right Click in it and then select
Web Service Client Resources àcall Web Service Operation

Java Client file

Now select the WebService name->Web Service port nameàWeb Service Operation Name

Java Client file

Click on Ok .
It generates the code
In this code give int x=5;
Now run this file .It will run the file call the web service and
Print the result as the factorial .

package pack1;

public class Client1 {
  public static void main(String[] args) {
  try {
  pack1.FactorialService service = new pack1.FactorialService();
  pack1.Factorial port = service.getFactorialPort();
  int x = 5;
    java.lang.String result = port.fact1(x);
  System.out.println("Result = "+result);
  } catch (Exception ex) {
  ex.printStackTrace();
  }
  }
}

Java Client file

Download Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 21, 2009

Related Tutorials

Discuss: Java Client webservice   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Aishwarya Goyal
April 8, 2011
giving values at runtime in a web service

Hello Sir I tried the 2 examples on webservice given in this site(hello and factorial)an am able to execute them successfully.I would like to know that how can we give values at run time to a web service?I mean , here the parameter is passed in a jsp page..Is it possible that parameter be passed through a browser?
DMCA.com