Web Srvice Manual
Web Srvice Manual
Algorithm
A web service includes three basic components:
1. A mechanism to find and register interest in a service
2. A definition of the services input and output parameters
3. A transport mechanism to access a service
There are a few steps involved in developing a web service using it. These steps can be
summarized as follows:
i. Start the Net beans IDE; go to the New Project which is available under File
menu. The New Project wizard opens
ii. Select the web from categories options and web application from project section
and then press the next button.
Figure One: Create New Project Step 1
iii. 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 web application 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.
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 another is
through source mode. In our example we use design mode for creating skeleton of
WebMethod in easiest way.
Figure Five: Add Operation To Web Service
i. As we can see from highlighted section in the figure five we can add web service
operations by clicking Add Operation button. It opens a Add Operation dialog
box. Please refer to figure six.
ii. In the Add Operation dialog we must mention name (which is actually a
WebMethod name).
iii. 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. and parameter2) of
type double. Similarly we create other operations as well like
Addition,subtraction, multiplication, division
Figure Six: Add Parameters To Web Operation
Then we switch from design mode to source mode as shown in figure seven to do rest of
the implementation.
package calc.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import calc.util.NumberFormater;
@WebService()
public class JSimpCalcWebService {
package calc.util;
import java.text.NumberFormat;
public class NumberFormater {
Now our web service is ready deployment and test. 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. :
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.
Figure Ten: Test Web Service
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 eleven.
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:
i. Start Visual Studio 2008; go to the New Web Site which is available under
File menu.
Now we need to mention the WSDL file in our web site. To add the web service reference
we must perform the following steps:
iv. Copy and paste the WSDL URL from our web browers address bar (refer to
figure twelve) to Add Web Reference dialogs address bar and press go button
(refer to figure fifteen).
Using C# we can very easily invoke the web service with few lines of code:
i. We first design an ASP.net page. The default fie name is Default.aspx (the source
is available in zip file).
ii. Induce the web reference in to our code (i.e. Default.aspx.cs). For example:
using JSimpCalcWebService;
iii. Next, create the object of the web reference.
proxy.addition(10,20);
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JSimpCalcWebService;
try
{
Label2.Text = "Result: " +
proxy.Add(double.Parse(TextBox1.Text),double.Parse(TextBox2.Text));
}
catch (FormatException)
{
Label2.Text = "Result: Invalide Input";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
Label2.Text = "Result: " + proxy.Sub(double.Parse(TextBox1.Text),
double.Parse(TextBox2.Text));
}
catch (FormatException)
{
Label2.Text = "Result: Invalide Input";
}
}
protected void Button3_Click(object sender, EventArgs e)
{
try
{
Label2.Text = "Result: " + proxy.Mul (double.Parse(TextBox1.Text),
double.Parse(TextBox2.Text));
}
catch (FormatException)
{
Label2.Text = "Result: Invalide Input";
}
}
protected void Button4_Click(object sender, EventArgs e)
{
try
{
Label2.Text = "Result: " + proxy.Division (double.Parse(TextBox1.Text),
double.Parse(TextBox2.Text));
}
catch (FormatException)
{
Label2.Text = "Result: Invalide Input";
}
}
}
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.
Figure Seventeen: Web Site Output
1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Java
Web category. Name the project WebServiceJSP. Click Finish.
2. Right-click the WebServiceJSP node and choose New > Web Service Client.
3. In Project, click Browse. Browse to the web service that you want to consume.
When you have selected the web service, click OK.
The Projects window displays the new web service client, as shown below:
5. In the Web Service References node, expand the node that represents the web
service. The Addition operation, which will invoke from the client, is now
exposed.
6. Drag the Addition operation to the client's index.jsp page, and drop it
below the H1 tags. The code for invoking the service's operation is now generated
in the index.jsp page, as you can see here:
integers, such as 3 and 4. Replace the commented out TODO line in the catch
block with out.println("exception" + ex);.
The server starts, if it wasn't running already. The application is built and
deployed, and the browser opens, displaying the calculation result:
8. <%
9. try {
10. org.me.calculator.CalculatorWSService service =
11. new org.me.calculator.CalculatorWSService();
12. org.me.calculator.CalculatorWS port =
13. service.getCalculatorWSPort();
14. // TODO initialize WS operation arguments here
15. int i = 0;
16. int j = 0;
17. // TODO process result here
18. int result = port.add(i, j);
19. out.println("Result = "+result);
20. } catch (Exception ex) {
21. // TODO handle custom exceptions here
22. } %>