Creating Java Web Service

1.  Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Web category or EJB Module from the Enterprise category.

2.  Name the project CalculatorWSJava.

3.  Depending on the deployment server that you want to use, do the following:

For GlassFish, set the Java EE Version to Java EE 5.

For the Tomcat Web Server, choose J2EE 1.4, unselect the Set Source Level to 1.4 checkbox.

4.  Click through the remaining pages and click Finish.

5.  Right-click the CalculatorWebService node and choose New > Web Service.

6.  Name the web service CalculatorWS, type org.me.calculator in Package, and click Finish.

The Projects window displays the structure of the new web service and the visual designer is shown in the editor area.

7. Click Add Operation in the visual designer. A dialog box appears where you can define the new operation.

8. Click Source and notice that the source code that has been generated in the previous steps is as follows:

9. Change and add the method as below in CalculatorWS:

@WebMethod(operationName = "add")

public double addx(@WebParam(name = "i")

double i, @WebParam(name = "j")

double j) {

//TODO write your implementation code here:

double k = i + j;

return k;

}

@WebMethod(operationName = "min")

public double minx(@WebParam(name = "i")

double i, @WebParam(name = "j")

double j) {

//TODO write your implementation code here:

double k = i - j;

return k;

}

@WebMethod(operationName = "mul")

public double mulx(@WebParam(name = "i")

double i, @WebParam(name = "j")

double j) {

//TODO write your implementation code here:

double k = i * j;

return k;

}

@WebMethod(operationName = "div")

public double divx(@WebParam(name = "i")

double i, @WebParam(name = "j")

double j) {

//TODO write your implementation code here:

double k = i / j;

return k;

}

1.  Right-click the project and choose Deploy. The IDE starts the application server, builds the application, and deploys the application to the server. You can follow the progress of these operations in the CalculatorWSJava (run-deploy) and GlassFish V2 or Tomcat tabs in the Output view.

2.  In the IDE's Projects tab, expand the Web Services node of the CalculatorWSJava project. Right-click the CalculatorWS node, and choose Test Web Service.

3.  The IDE opens the tester page in your browser, if you deployed a web application to GlassFish. For the Tomcat Web Server and deployment of EJB modules, the situation is different:

If you deployed to GlassFish, type two numbers in the tester page, as shown below:

If deployed to Tomcat, the page will shown as below:

**** You can click on (WSDL File) and get the URL of the Web Service because we will use this URL for our C#.Net client.

Consuming Java Web Server from C#.NET Client

1.  Open Visual Studio 2010, Choose File > New Project (Ctrl-Shift-N).

2.  Under C# tab Select Windows -> Windows Forms Application and give a name to your project. Click on OK.

3.  You can add 3 label, 3 text box and 4 buttons for a basic calculator as follow:

4.  Now it is time to add our Java Web Service in to our client. In order to add Java Web Service right clicks on CalculatorClient6 in Solution Window and select Add Service Reference.

As I mentioned, we need to get the URL for our Java Web Service by clicking (WSDL File) link. After clicking on the link the page will be seen as follows:

Copy the URL from browser. For my example the URL is:

http://localhost:1010/CalculatorWSJava/CalculatorWS?wsdl

5.  Paste the URL and click on Go.

6.  Now you can see all of your methods. Now click on OK. (You can change web reference name)

7.  ** You have to pay attention of the name of the description because we will use it to create object to use our Web Service. In this example, the description name is CalculatorWSJavaReference.

8.  Double click on Form1 and go to source code.

9.  Add using CaculatorClient6.CalculatorWSJavaReference;

10.  Create an object from Web Service using:

CalculatorWSClient proxy = new CalculatorWSClient();

Now, you can double click on each button and call your methods by using this line:

textBox3.Text = proxy.add(Double.Parse(textBox1.Text),Double.Parse(textBox2.Text)).ToString();

11. Now, you can build and compile your C# Client.