![]() |
Languages »
Java »
General
Advanced
License: The Code Project Open License (CPOL)
How to invoke Java web service in ASP.net using C#By Bikash ShawWeb Services Interoperability |
Java, Java (J2EE), Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
According to W3C: A Web Service is a software system designed to support interoperable machine-to-machine interaction over a network:
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 other services. 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 web service 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 web service and Visual Studio 2008 for the ASP.net web service client which invokes the 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:

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



In our example the web service name is JSimpCalcWebService and the package name is calc.ws.
After we add web service to our application, now it's time to add web service operation 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.


WebMethod name). 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. and parameter2) of type double. Similarly we create other operations as well like subtraction, multiplication, division, power, maximum, minimum.
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.
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, int minFractionDigits, method to format the
int maxFractionDigits)double value to java.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
Now our web service is ready deployment and test. With Netbeans 6 it can achieved very with few steps. First make sure that the GlassFish server is running. To start the server we need to perform the following steps.

Now, as our server is running it's time to deploy the application and test the web service that we have developed. Netbeans does the deployment for us with few mouse clicks, mentioned as follows:
JSimpCalcWebService). A context menu pops up. 
The above mentioned steps deploy the application and lunch the default browser in which the web service can be tested via SOAP request and response. Please refer to the figure ten for sample output. We can also view the WSDL (Web Services Description Language) file by clicking on the hyperlink.

Alternatively we can test the web service and view its WSDL document through our GlassFish application server’s admin consol as shown in figure eleven.

Once we click on the View WSDL link we can view a XML file for describing our web service. The WSDL file should look like the figure twelve.

The URL shown at the address bar will require invoking the web service. We shall demonstrate its usage during creating of our web application using ASP.net.
Now, our web service is ready to get invoked from non Java based development platform. In this article we develop a sample ASP.net based client. Using Visual Studio 2008 it can be achieved in few steps, which can be summarized as follows:

JSimpCalcWebServiceWebSite Now we need to mention the WSDL file in our web site. To add the web service reference we must perform the following steps:
JSimpCalcWebServiceWebSite). A context menu pops up. 

JSimpCalcWebService) and click the Add Reference button. Using C# we can very easily invoke the web service with few lines of code:
using JSimpCalcWebServiceService;
JSimpCalcWebServiceService.JSimpCalcWebServiceService proxy = new JSimpCalcWebServiceService.JSimpCalcWebServiceService();
WebMethod like any other method call. For example:
proxy.addition(10,20);

using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using JSimpCalcWebServiceService; public partial class _Default : System.Web.UI.Page { JSimpCalcWebServiceService.JSimpCalcWebServiceService proxy; protected void Page_Load(object sender, EventArgs e) { proxy = new JSimpCalcWebServiceService.JSimpCalcWebServiceService(); } protected void btnAddition_Click(object sender, EventArgs e) { try { lblResultAddition.Text = "Result: " + proxy.addition(double.Parse(txtbtnAdditionParameter1.Text), double.Parse(txtbtnAdditionParameter2.Text)); } catch (FormatException) { lblResultAddition.Text = "Result: Invalide Input"; } UpdatePanelAddition.Update(); } protected void btnSubtraction_Click(object sender, EventArgs e) { try { lblResultSubtraction.Text = "Result: " + proxy.subtraction(double.Parse(txtSubtractionParameter1.Text), double.Parse(txtSubtractionParameter2.Text)); } catch(FormatException) { lblResultSubtraction.Text = "Result: Invalide Input"; } UpdatePanelSubtraction.Update(); } protected void btnMultiplication_Click(object sender, EventArgs e) { try { lblResultMultiplication.Text = "Result: " + proxy.multiplication(double.Parse(txtMultiplicationParameter1.Text), double.Parse(txtMultiplicationParameter2.Text)); } catch (FormatException) { lblResultMultiplication.Text = "Result: Invalide Input"; } UpdatePanelMultiplication.Update(); } protected void btnDivision_Click(object sender, EventArgs e) { try { lblResultDivision.Text = "Result: " + proxy.division(double.Parse(txtDivisionParameter1.Text), double.Parse(txtDivisionParameter2.Text)); } catch (FormatException) { lblResultDivision.Text = "Result: Invalide Input"; } UpdatePanelDivision.Update(); } protected void btnMaximum_Click(object sender, EventArgs e) { try { lblResultMaxMin.Text = "Result: " + proxy.maximum(double.Parse(txtMaxMinParameter1.Text), double.Parse(txtMaxMinParameter2.Text)); } catch (FormatException) { lblResultMaxMin.Text = "Result: Invalide Input"; } UpdatePanelMaxMin.Update(); } protected void btnMinimum_Click(object sender, EventArgs e) { try { lblResultMaxMin.Text = "Result: " + proxy.minimum(double.Parse(txtMaxMinParameter1.Text), double.Parse(txtMaxMinParameter2.Text)); } catch (FormatException) { lblResultMaxMin.Text = "Result: Invalide Input"; } UpdatePanelMaxMin.Update(); } protected void btnPower_Click(object sender, EventArgs e) { try { lblResultPower.Text = "Result: " + proxy.power(double.Parse(txtPowerParameter1.Text), double.Parse(txtPowerParameter2.Text)); } catch (FormatException) { lblResultPower.Text = "Result: Invalide Input"; } UpdatePanelPower.Update(); } }Source Three: Default.aspx.cs Source Code
Now, it's time to test our web service client application by clicking on the Start Debugging toolbar button or by pressing F5 key. The web page should look like the figure below.

This article tried to demonstrate standards used by web services with little concern for the underlying implementation mechanism. The example shown here exploits a number of properties of web services mentioned below:
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Jan 2009 Editor: Sean Ewington |
Copyright 2009 by Bikash Shaw Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |