10/18/2011 1:18:53 PM
Viewed: 2673
teanjava
9/20/2011 2:47:09 AM
How to invoke Java web service in ASP.net using C#

Contents:

Background:

According to W3C: A Web Service is a software system designed to support interoperable machine-to-machine interaction over a network:

  1. It has an interface described in a machine-processable format (specifically WSDL).
  2. Other systems interact with the web service in a manner prescribed by its description using SOAP messages, typically conveyed using HTTP with an XML serialization in conjunction with other Web standards.
Features of web services:
  1. Web services operate over any network (the Internet or a private Intranet) to achieve specific tasks.
  2. The tasks performed by a web service are methods or functions that other applications can invoke and use.
  3. Web service requests/responses can be sent/received between different applications on different computers belonging to different businesses.
A web service includes three basic components:
  1. A mechanism to find and register interest in a service
  2. A definition of the service’s input and output parameters
  3. A transport mechanism to access a service
Web services also include other technologies that can be used to provide additional features such as security, transaction processing and others.

Web Services Interoperability:

Much of the promise of web services is its potential for seamless interoperability across heterogeneous systems, platforms, applications, and programming languages. Interoperability is a primary goal of web services. However, it is not a given. Web services standards facilitate interoperability, but do not ensure it. Many considerations and issues need to be resolved to achieve full interoperability. As the number of specifications expands to address gaps, so do the interoperability challenges.

A web service has many potential clients, and this array of clients can use a variety of libraries and providers to connect. Services can, in turn, become clients of otherservices. Ensuring that clients based on different implementations of the standards can interoperate with the service is critical to the service's success.

The primary goal of this article is to demonstrate how to create Java webservice and invoke it from ASP.net (C#) application.
Here we shall develop simple mathematical functions like addition, subtraction etc. using Java web services called JSimpCalcWebService. Later we shall demonstrate how to invoke these web service methods through ASP.net with C# called JSimpCalcWebServiceWebSite.To create there two separate applications we need to use two IDEs. Here in this example we use NetBeans 6 for Java webservice and Visual Studio 2008 for the ASP.net web service client which invokes the web service.

Create a Java Web Service:

Netbeans 6 provides vary easy and convenient way to develop web service. There are a few steps involved in developing a web service using it. These steps can be summarized as follows:

  1. Create a web application project.
  2. Add web service to the project.
  3. Add operations to the web service.
  4. Implementing the web methodes.
  5. Deploy and test the web service.

Step 1. Create a web application project

  1. Start the Netbeans IDE; go to the New Project which is available under File menu. The New Project wizard opens.
  2. Select the web from categories options and web application from project section and then press the next button.

    01.CreateWebServiceNetbeansChooseWebAppProject.png
    Figure One: Create New Project Step 1
  3. On the next screen mention the project name, select the project location. We can also mention the server name in which we want to deploy our webapplication as well we can change the default context path.

    Here we mention the project name JSimpCalcWebService and keep the context path same as project name. We use GlassFish V2 application server for deployment.

    Create Web Service Netbeans Project
    Figure Two: Create New Project Step 2
  4. Now, click the finish button.

Step 2. Add web service to the project

  1. Right click on the project name in the Projects explorer window.

    Create Web Service Netbeans Menu
    Figure Three: Web Service Menu Option
  2. From the context menu options select the Web Service menu. A web servicedialog opens.

    Create Web Service Netbeans Web Serice Name
    Figure Four: Add New Web Service
  3. Mention the web service name and the package name as mentioned in the image below, and then click the finish button.

    In our example the web service name is JSimpCalcWebService and the package name is calc.ws.

Step 3. Add operations to the web service

After we add web service to our application, now it's time to add web serviceoperation or WebMethod. We can do it in two possible ways one is through design mode and anather is through source mode. In our example we use design mode for creating skeleton of WebMethod in easiest way.

Create Web Service Netbeans WS Add Operation Button
Figure Five: Add Operation To Web Service
  1. As we can see from highlighted section in the figure five we can add webservice operations by clicking Add Operation button. It opens a Add Operation dialog box. Please refer to figure six.

    Create Web Service Netbeans WS Add Operation
    Figure Six: Add Parameters To Web Operation
  2. In the Add Operation dialog we must mention name (which is actually aWebMethod name).
  3. We can also enter parameter names and their types (these parameter are known as WebParam).

    In the figure six we mention the WebMethod name as addition whose return type is java.lang.String and it takes two parameters (parameter1. andparameter2) of type double. Similarly we create other operations as well like subtraction, multiplication, division, power, maximum,minimum.

Step 4: Implementing the web methodes

Once we finish step three, a basic structure of our web service should be ready. Then we switch from design mode to source mode as shown in figure seven to do rest of the implementation.

Create Web Service Netbeans WS Source Mode
Figure Seven: Write The Code For The Web Service

The code should look like as follows:

package calc.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import calc.util.NumberFormater;

/**
* @author Bikash Shaw
*/

@WebService()
public class JSimpCalcWebService {

/**
* Web service operation
*/

@WebMethod(operationName = "addition")
public String addition(@WebParam(name = "parameter1")
double parameter1, @WebParam(name = "parameter2")
double parameter2) {
//TODO write your implementation code here:
return NumberFormater.format((parameter1 + parameter2),0,6);
}

/**
* Web service operation
*/

@WebMethod(operationName = "subtraction")
public String subtraction(@WebParam(name = "parameter1")
double parameter1, @WebParam(name = "parameter2")
double parameter2) {
//TODO write your implementation code here:
return NumberFormater.format((parameter1 - parameter2),0,6);
}

/**
* Web service operation
*/

@WebMethod(operationName = "multiplication")
public String multiplication(@WebParam(name = "parameter1")
double parameter1, @WebParam(name = "parameter2")
double parameter2) {
//TODO write your implementation code here:
return NumberFormater.format((parameter1 * parameter2),0,6);
}

/**
* Web service operation
*/

@WebMethod(operationName = "division")
public String division(@WebParam(name = "parameter1")
double parameter1, @WebParam(name = "parameter2")
double parameter2) {
//TODO write your implementation code here:
return NumberFormater.format((parameter1 / parameter2),0,6);
}

/**
* Web service operation
*/

@WebMethod(operationName = "power")
public String power(@WebParam(name = "parameter1")
double parameter1, @WebParam(name = "parameter2")
double parameter2) {
//TODO write your implementation code here:
return NumberFormater.format(Math.pow(parameter1, parameter2),0,6);
}

/**
* Web service operation
*/

@WebMethod(operationName = "maximum")
public String maximum(@WebParam(name = "parameter1")
double parameter1, @WebParam(name = "parameter2")
double parameter2) {
//TODO write your implementation code here:
return NumberFormater.format(Math.max(parameter1, parameter2),0,6);
}

/**
* Web service operation
*/

@WebMethod(operationName = "minimum")
public String minimum(@WebParam(name = "parameter1")
double parameter1, @WebParam(name = "parameter2")
double parameter2) {
//TODO write your implementation code here:
return NumberFormater.format(Math.min(parameter1, parameter2),0,6);
}
}
Source One: Java JSimpCalcWebService.java Web Service Source Code

Here we use NumberFormater.format(double number, intminFractionDigits,
int maxFractionDigits)
 method to format the double value tojava.lang.String up to six decimal point. Refer to the source two.

package calc.util;

import java.text.NumberFormat;

/**
* @author Bikash Shaw
*/

public class NumberFormater {

public static String format(double number, int minFractionDigits,
int maxFractionDigits) {
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(maxFractionDigits);
format.setMinimumFractionDigits(minFractionDigits);
return format.format(number);
}
}
Source Two: NumberFormater.java Source Code

Step 5. Deploy and test the 

Having Tea And Feeling Java

Gửi trả lời