0% found this document useful (0 votes)
51 views19 pages

Web Srvice Manual

This document describes how to create an arithmetic web service in Java that can be invoked from .NET and JSP. It involves 5 steps: 1) Create a web application project in Java, 2) Add a web service to the project, 3) Add operations to the web service, 4) Implement the web methods, 5) Deploy and test the web service. It then describes how to create an ASP.NET website client to invoke the Java web service by adding a web reference and calling the web methods.

Uploaded by

suganya004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views19 pages

Web Srvice Manual

This document describes how to create an arithmetic web service in Java that can be invoked from .NET and JSP. It involves 5 steps: 1) Create a web application project in Java, 2) Add a web service to the project, 3) Add operations to the web service, 4) Implement the web methods, 5) Deploy and test the web service. It then describes how to create an ASP.NET website client to invoke the Java web service by adding a web reference and calling the web methods.

Uploaded by

suganya004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

LAB Exercise -9 Developing Arithmetic web Service in JAVA to invoke

it in DOTNET and JSP

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

Create a Java 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

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.

Figure Two: Create New Project Step 2


iv. Now, click the finish button.

Step 2. Add web service to the project

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


From the context menu options select the Web Service menu. A web service dialog
opens.
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.

Figure Three: Web Service Menu Option


Figure Four: Add New Web Service

Step 3. Add operations to the web service

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

Step 4: Implementing the web methodes

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;

@WebService()
public class JSimpCalcWebService {

/** * Web service operation */


@WebMethod(operationName = "Add")
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 = "Sub")
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 = "Mul")
public String multiplication(@WebParam(name = "parameter1")
int parameter1, @WebParam(name = "parameter2")
int 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);
}
}

Source One: Java JSimpCalcWebService.java Web Service Source Code

Here we use NumberFormater.format(double number, int minFractionDigits,


int maxFractionDigits) method to format the double value to java.lang.String up to six
decimal point. Refer to the source two.

package calc.util;
import java.text.NumberFormat;
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 web service

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.

i. Go to Services explorer window.


ii. Expand the Servers node.
iii. Right click on the server name (in our case its GlassFish V2). A context menu
pops up.
iv. Select Start from the menu options.
Figure Eight: Start GlassFish V2 Application Server

Now, as our server is running it's time to deploy the application and test the web service
that we have developed. :

i. Go to Projects explorer window.


ii. Right click on the project JSimpCalcWebService ,Click deploy option
from the context meu pops up
iii. Expand the Web Services node.
iv. Right click on the web services name (in our case its JSimpCalcWebService).
A context menu pops up.
v. Click the Test Web Service menu.
Figure Nine: Test Web Service

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.

Figure eleven Twelve: WSDL File


The URL shown at the address bar will require invoking the web service.

Creating of our web application using ASP.net.

Create ASP.net Web Site Using C#:

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:

1. Create ASP.net web site.


2. Add web reference.
3. Write code to invoke web service.
4. Test web service client application.

Step 1. Create ASP.net web site

i. Start Visual Studio 2008; go to the New Web Site which is available under
File menu.

Figure Thirteen: New Web Site Window


ii. Select ASP.net Web Site.
iii. Select the folder in which we want to create the web site. In our case its
JSimpCalcWebServiceWebSite
iv. Select the language Visual C# and click OK button.

Step 2. Add web reference

Now we need to mention the WSDL file in our web site. To add the web service reference
we must perform the following steps:

i. Go to Solution Explorer window.


ii. Right click on the project name (in our case its
JSimpCalcWebServiceWebSite). A context menu pops up.
iii. Click the Add Web Reference menu. The Add Web Reference dialog opens

Figure Fourteen: Add Web Reference Context Menu

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).

Figure Fifteen: Add Web Reference Dialog


v. We can see all the methods names of our web service. Give a name to the web
reference (in this example its JSimpCalcWebService) and click the Add
Reference button.

Step 3. Write code to invoke web service

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.

JSimpCalcWebService.JSimpCalcWebServiceService proxy = new


JSimpCalcWebService.JSimpCalcWebServiceService();
iv. Now, we can access the WebMethod like any other method call. For example:

proxy.addition(10,20);

Figure Sixteen: Web Methods Invocation Code


The code should look like as follows:

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;

public partial class _Default : System.Web.UI.Page


{
JSimpCalcWebService.JSimpCalcWebServiceService proxy;
protected void Page_Load(object sender, EventArgs e)
{
proxy = new JSimpCalcWebService.JSimpCalcWebServiceService();
}
protected void Button1_Click(object sender, EventArgs e)
{

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

Step 4. Test web service client application

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

Client 5: JSP Page in Web Application

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.

4. Leave the other settings at default and click Finish.

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:

Change the value for paramerter1 and paramerter2 from 0 to other

integers, such as 3 and 4. Replace the commented out TODO line in the catch
block with out.println("exception" + ex);.

7. Right-click the project node and choose Run.

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. } %>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy