Class: SYMCA Subject: Lab I- Cloud Computing
Experiment No. 5
Aim: To study & Implement Web services in SOAP for JAVA Applications.
Theory:
Overview of Web Services
Web services are application components that are designed to support interoperable machineto-
machine interaction over a network. This interoperability is gained through a set of XMLbased open
standards, such as the Web Services Description Language (WSDL), the Simple
Object Access Protocol (SOAP), and Universal Description, Discovery, and Integration (UDDI).
These standards provide a common and interoperable approach for defining, publishing, and
Using web services.
Choosing a Container:
You can either deploy your web service in a web container or in an EJB container. This depends
On your choice of implementation. If you are creating a Java EE application, use a web container
In any case, because you can put EJBs directly in a web application. For example, if you plan to
Deploy to the Tomcat Web Server, which only has a web container; create a web application, not
An EJB module.
Choose File > New Project. Select Web Application from the Java Web category
.Name the project Calculator WS Application. Select a location for the
Project. Click Next.
Select your server and Java EE version and click Finish.
Creating a Web Service from a Java Class
Right-click the Calculator WS Application node and choose New > Web
Service.
Name the web service Calculator WS and type org.me.calculator in
Package. Leave Create Web Service from Scratch selected.
If you are creating a Java EE project on Glass Fish or WebLogic, select Implement Web
Service as a Stateless Session Bean.
Click Finish. The Projects window displays the structure of the new web service and the
Source code is shown in the editor area.
Adding an Operation to the Web Service
The goal of this exercise is to add to the web service an operation that adds two numbers received
From a client. The NetBeans IDE provides a dialog for adding an operation to a web service. You
Can open this dialog either in the web service visual designer or in the web service context menu.
To add an operation to the web service:
Change to the Design view in the editor.
Click Add Operation in either the visual designer or the context menu. The Add
Operation dialog opens.
in the upper part of the Add Operation dialog box, type add in Name and type int in
The Return Type drop-down list.
in the lower part of the Add Operation dialog box, click Add and create a parameter of
Type int named i.
Click Add again and create a parameter of type int called j.
Click OK at the bottom of the Add Operation dialog box. You return to the editor.
Remove the default hello operation, either by deleting the hello () method in the source
Code or by selecting the hello operation in the visual designer and clicking remove
Operation.
The visual designer now displays the following:
Click Source and view the code that you generated in the previous steps. It differs whether you
created the service as a Java EE stateless bean or not. Can you see the difference in the
screenshots below? (A Java EE 6 or Java EE 7 service that is not implemented as a stateless
bean resembles a Java EE 5 service.)
Note. In NetBeans IDE 7.3 and 7.4 you will notice that in the generated @WebService annotation the
service name is specified explicitly:@WebService(serviceName = "CalculatorWS").
9. In the editor, extend the skeleton add operation to the following (changes are in bold):
@WebMethod
public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
int k = i + j;
return k;
As you can see from the preceding code, the web service simply receives two numbers and then
returns their sum. In the next section, you use the IDE to test the web service.
Deploying and Testing the Web Service
After you deploy a web service to a server, you can use the IDE to open the server's test client, if
the server has a test client. The GlassFish and WebLogic servers provide test clients.
If you are using the Tomcat Web Server, there is no test client. You can only run the project and
see if the Tomcat Web Services page opens. In this case, before you run the project, you need to
make the web service the entry point to your application. To make the web service the entry point
to your application, right-click the CalculatorWSApplication project node and choose Properties.
Open the Run properties and type /CalculatorWS in the Relative URL field. Click OK. To run
the project, right-click the project node again and select Run.
To test successful deployment to a GlassFish or WebLogic server:
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 CalculatorWSApplication (run-deploy) and the GlassFish server or
Tomcat tabs in the Output view.
2. In the IDE's Projects tab, expand the Web Services node of the
CalculatorWSApplication project. Right-click the CalculatorWS node, and
choose Test Web Service.
The IDE opens the tester page in your browser, if you deployed a web application to the
GlassFish server. For the Tomcat Web Server and deployment of EJB modules, the
situation is different:
If you deployed to the GlassFish server, type two numbers in the tester page, as shown
below:
Consuming the Web Service
Now that you have deployed the web service, you need to create a client to make use of the web
service's add method. Here, you create three clients— a Java class in a Java SE application, a
servlet, and a JSP page in a web application.
Note: A more advanced tutorial focusing on clients is Developing JAX-WS Web Service
Clients.
Client 1: Java Class in Java SE Application
In this section, you create a standard Java application. The wizard that you use to create the
application also creates a Java class. You then use the IDE's tools to create a client and consume
the web service that you created at the start of this tutorial.
Choose File > New Project (Ctrl-Shift-N on Linux and Windows, ⌘-Shift-N on MacOS).
Select Java Application from the Java category. Name the
projectCalculatorWS_Client_ Application. Leave Create Main Class selected and
accept all other default settings. Click Finish.
Right-click the CalculatorWS_Client_Application node and choose New > Web
Service Client. The New Web Service Client wizard opens.
Select Project as the WSDL source. Click Browse. Browse to the CalculatorWS web service
in the CalculatorWSApplication project. When you have selected the web service, click OK.
Do not select a package name. Leave this field empty.
Leave the other settings at default and click Finish.
The Projects window displays the new web service client, with a node for the add method
that
you created:
Double-click your main class so that it opens in the Source Editor. Drag the add node
below the main() method.
Note: Alternatively, instead of dragging the add node, you can right-click in the editor
and then choose Insert Code > Call Web Service Operation.
8. In the main() method body, replace the TODO comment with code that initializes
values for i and j, calls add(), and prints the result.
9. public static void main(String[]
args)
{ int i = 3;
int j = 4;
int result = add(i, j);
System.out.println("Result = " +
result);
Right-click the project node and choose Run.
The Output window now shows
the sum: compile:
run:
Re sul t = 7
BUILD SUCCESSFUL (total time: 1 second)
Conclusion:
Thus we have studied use of webservices using SOAP for a java application