Skip to main content

What is Web Service?

What is Web Service?

A piece of code or an application which you can expose it to the users (a web page, an application, a machine, etc.) to use it, is called a web service. It doesn’t matter what the length of the code is, but the number of functionalities (services) one accesses is what it counts. For example, an inventory system accessing weather information to predict the storage of goods.

Why do we need Web Service?

When you receive the service, it may be free of charge or you need to pay for it. Let’s see the analogy. If you build the house and want the electricity connection, will you built up new thermal power station to satisfy your needs or will you go to the producer and pay the monthly bills? Same way, when we want a search feature in our application, shall we write a code from the scratch or use it from someone who has already built it, like Google?

Where can I find the Web Service?

If you have ever stumbled upon the site called www.w3schools.com then you have tried it knowingly or unknowingly. If not, visit it and find one white rectangular box in the top right corner and you will see Powered by Google inscribed in it. Here, W3CSchools is using the service provided by Google and not its own. Simple thing is why to reinvent a wheel when it is already present.

How it came into existence?

This is a good question. Few decades ago there were applications running on standalone computers or on the computer which were interconnected somehow. The problem was the communication among the independent applications. Two applications having the same or complementary services were not able to communicate just because they were written on different platforms. Let’s take the scenario to understand this problem. There is a company called ‘A’ which is a global leader in production of computer systems. It maintains its accounts using computers. After few years it grew very much and thought to acquire a company called ‘B’ which was doing quite good in the market so that it can expand its business. Both the companies signed a deal without any hassles. After some time, to their surprise, they confronted a technology problem; their technical departments were not able to merge two different applications which were running in their respective companies. The only solution was to spend more time, more money, and more man power to build the same application on the platform which company ‘A’ can understand. The solution to such problem is a common platform which all the systems can understand one another irrespective of the programming languages in which they are developed.

How to build a Web Service?

One can build the web service using any programming language. For example, user ‘A’ develops it using Java and user ‘B’ in .net. There are other programming languages also like Ruby on Rails, Python, PHP, etc. which can be used to develop the web service.

How do they communicate then?

Though they are written in different programming languages, the can have interaction. The only thing they need is a common language. This means, the applications must be able to produce and consume that common language.

Which Common Language does Web Service have?

We can use the widely acceptable language called XML. It stands for eXtensible Markup Language. It is similar to HTML (Hyper Text Markup Language) but different in some aspects and has its self-described tags which HTML lacks. HTML is used for the presenting the content that server sends to the client (browser) which interprets the content to display in a manner it was intended to do so whereas XML is for transferring and storing data which can be consumed by any application that can process it. The advantage is one can do any type of operation one wants on the XML data. Let’s see the difference between them.

HTML

<h1> the text to display </h1>

XML

<header1> the text to transfer or store </header1>
Here, the tag <h1> is already defined in the standards which browser follows. Browser knows that whenever it receives this tag it has to bold and increase the size of text. In XML, the tag called <header1> is not defined anywhere. It has no definition of itself stored at any place. When browser receives such tag it will just display it without manipulating the text. Then who will process it correctly? The application which consumes it.

Does it follow any architecture?

Yes. As we follow methodologies and principles of various models like waterfall, RAD (Rapid Application Development) to develop the project, Web Service also has various architectures. We will limit our discussion by understanding the SOA (Service Oriented Architecture) and ROA (Resource Oriented Architecture). It can follow either of both.

SOA (Service Oriented Architecture)

As its name suggests, it focuses mainly on services. The service can be whole application or any piece of code. See the below diagram of SOA.




1.  Service Provider: It is the one which develops the service so that other applications can use it. For example, search feature from Google can be used as a web service. So here Google will act as a Service Provider.

2.  Service Broker: It is a platform where Service Provider can publish his service and Service Consumer can find it. For example, UDDI (Universal Description, Discovery and Integration) is a XML-based registry where Service Provider lists his services and lets Service Consumer to discover the available services in it.

3. Service Consumer: It is the one which uses the service. For example, www.w3schools.com uses the search service of Google.

Steps:
  1.   Service Provider will develop the service and test it.
  2.  Service Provider then publishes it in the registry.
  3.  Service Broker will classify the service based upon the functionality it offers.
  4.  Service Consumer will find the service in Service Broker’s registry by entering the keywords such as functionality name, company name, etc.
  5.   Service Consumer will contact Service Provider and negotiates the terms and conditions.
  6.   Once both the parties agree to each other, Service Provider will give access of its service to Service Consumer.
  7.  Service Consumer will download the necessary files from the Service Broker’s registry and try to communicate with the Service Provider. This step is repeated only when the service is updated.


So, the above steps were how to consume the service. But what does it consume and how? Whenever the Service Provider publishes the services, it is not the original code which he will upload it. It will be just an XML document called WSDL (Web Service Description Language). This is the document that Service Consumer will download on its side to use the service. Once you have WSDL, you need to transfer and get some data to/from the service. This done using SOAP (Simple Object Access Protocol).

WSDL

As said, it represents the code of a service in XML format. It is W3C recommendation and helps to locate web services. It states the number and type of parameters Service Consumer needs to send to the service; what will be return type of the message, etc. This file nowadays is easily generated by the editors like Ecllipse on behalf of Service Provider. Just write the code and Ecllipse will take care of WSDL.

SOAP

Whatever you send and receive in the form of stream of bytes between Service Provider and Service Consumer is nothing but a simple object. This object travels on the HTTP protocol. SOAP has its own structure to pack the stream of bytes (the message that Service Consumer and Service Provider exchange) and gets enveloped in the body part of the HTTP. SOAP is just a format for sending messages and it is written in XML. Below is the diagram of SOAP architecture.
The architecture shows the path of a SOAP request and a SOAP response between the SOAP Client and the SOAP Server. The SOAP request is sent from the system running the SOAP Client, going out over the Internet as a SOAP request. The SOAP server receives the request, possibly through a firewall, and passes the service request to the SOAP Service. The SOAP Server then sends the SOAP response over the Internet and back to the SOAP client.







ROA (Resource Oriented Architecture)

An architecture that mainly focuses on resources, is ROA. Then what is resource? Thing that can be addressed is liable to be called a resource. For example, when you write http://www.w3schools.com/php/default.asp in the browser, you are indirectly telling the browser to get the default.asp page which is hosted on the www.w3schools.com server. So, in short, the resource can be anything which has its own location to find out. See the below list of some possible resources:

  • Latest version of Ubuntu
  •  Some information about Java Web Services
  • The next even number after 10
  • The income in the fourth quarter of year


So how can one make the resource a resource? It has to have at least one URI (Uniform Resource Identifier). URI signifies the name and address of the resource. In our previous example, default.asp is the name of the resource and www.w3schools.com/php an address. If I upload my picture on Facebook, then it will have its own unique identifier generated by the application and then it is used to locate the image when it is viewed by you or your friends.

There are two main features of ROA:

Addressability

If you want to find home of your friend, you will take his address rather than searching every home one by one which is quite cumbersome job. Same way if one tells you to locate the resource called default.asp, will you first go to Google, then search for www.w3schools.com and then click on PHP link? Of course not. This is one of the way that human prefers. But would your application jump from one link to another? No. You can feed your application the URI www.w3schools.com/php/default.asp to find out the resource.

Statelessness

As you know HTTP is a stateless protocol which means every request to the server takes place in isolation. So, for instance, if you send the first request to fetch some resource and then again after some time you resend the same request, the server takes it as two different requests and not one. Every request is independent of each other. For example, when you search for Java Web Services on Google, you will not find whole bunch of links on the very first page. Only 10 links will be displayed first and rest of them are placed sequenced numerically from 1 to n. So if you click on the next button, the browser will send another request to the server to retrieve the next 10 links.






What are the different ways of developing Web Service?

Developing Web Service is possible through JAX-RPC and JAX-RS.

JAX-RPC

JAX-RPC stands for Java API for XML-based RPC (Remote Procedure Call). It is an API which developers can use to call the procedures which are placed at remote locations. It makes it easier by providing the programming model for the development of SOAP (Simple Object Access Protocol)-based applications. It helps to call the web service written completely in a different programming language and very helpful in distributed client/server model. This is possible because JAX-RPC uses technologies defined by W3C (World Wide Web Consortium) like SOAP, HTTP and WSDL. When the procedure/method is invoked, it gets converted into SOAP message and is transmitted via HTTP protocol. This means JAX-RPC follows SOA (Service Oriented Architecture).

JAX-RS

JAX-RS stands for Java API for RESTful (Representational State Transfer) web services. It defines a set of architectural principles by which data can be transmitted over HTTP protocol. One can use the web service using the URI of the resource and the server sends the representation of the resource as data. As it emphasizes more on the usage of HTTP, it utilizes the ROA (Resource Oriented Architecture). It uses the methods such as:

·         GET: to retrieve the resource
·         POST: to create a resource on the server
·         PUT: to change the state of resource or to update it
·         DELETE: to remove or delete a resource

It also uses the property called Statelessness of ROA for load-sharing. For example, there are three servers called A, B, and C. Server A can accept only 1000 requests at a time. Server B and C are sitting idle. Due to the peak period, Server A receives 2000 requests which may result into server crash. To avoid such situation, Server A distributes the load of extra 1000 requests among Server B and C. This is possible because every request is independent as it takes the benefits of URI. As there are no resource states maintained on any server, each server executes requests without worrying to find the previous states and sends the representation (in XML, JSON or both) of the resource directly to the client. Representation is an important property of REST. A resource is referenced by the URI and can be requested by an application in any type of representation that it supports. For example, browser needs the resource in HTML representation, JavaScript needs in JSON (JavaScript Object Notation) format and Java application may need it in XML.

Should I use JAX-RPC or JAX-RS?

JAX-RPC
·        
  • As it has WSDL, it can be used to define your web service. It defines the bindings, messages, operation and location of the web service.
  •   SOAP web services are useful in handling asynchronous processing and invocation.
  •  Use SOAP when web services are complex in nature as it transfers all the necessary information in the SOAP messages to call the remote procedures.
  • Supported in J2EE 1.4
  • Executes a method on stub, runtime system converts method invocation into message and transmit it as HTTP request

JAX-RS

  •  It provides a good caching infrastructure which improves the performance by serving the same requests from the cache memory and instead of downloading from the server.
  • It is particularly useful for restricted-profile devices like mobiles, tablets, etc. because the overhead of additional parameters like headers and other SOAP elements are less.
  • It is very simple compared to SOAP.
  • When CRUD (Create, Read, Update, Delete) are essential for the application.
  • Supported in JavaEE7.0
  • Contains a Java Programming language class files with JAX-RS annotation to define resources and actions can be performed on those resources

   By: Amit V. Chaudhary


Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Email/Password

In our  previous  article we saw how to create authentication module using Google identity provider. Now we will see how to implement Email and Password authentication using Firebase. We will use Bootstrap form to create intuitive user interface for sign-up and login using Email and Password. Step 1: Create signUp component. ng generate component signUp Step 2: Create custom form in sign-up.component.html file. The result of above code: Step 3: Add two functions for creating the user and login using Email and Password in src/app/providers/AFAuth.ts file. Step 4: Call createUserWithEmailAndPassword function in service from sign-up.component.ts. Step 5: Update routing configuration in app.module.ts  to include signUp component. Step 6: Update Login form to have Bootstrap form. The result of the above code. Step 7: Enable Email/Password component in Firebas...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...