You are on page 1of 50

Web Services

Updated by Dr Suthikshn Kumar


Suthikshn.kumar@pes.edu

1
Contents
• What is WS?
• SOAP
– RPC style SOAP
– Document Style SOAP
• WSDL
• REST Services
• JSON Format
– What is JSON?
– Array Literals
– Object Literals
– Mixing Literals
– JSON Syntax
– JSON Encoding and Decoding
– JSON vs XML

2
Introduction
• When XML use began to take off after 2000, businesses,
developers, and others looked for new ways to use it.
• The promise of separating content and presentation had been
met, but how could this capability be capitalized upon?
• The answer was web services.
• Web services provide a way to exchange data between
applications and services.
• To facilitate this communication, web services use the internet to
send messages composed of XML data back and forth between
the consumer and provider.
• The major difference with web services is that the data being
transmitted is XML text instead of a binary format.
• The promise behind web services is that of having software
components available on demand, to any application in the
world.
• Whether that be a web application or a traditional desktop
application, it is possible to use the same service to perform the 3
same task.
Related Technologies
• SOAP( Simple Object Access Protocol): is a
combination of an XML-based language and any
number of common protocols for transmitting
this data.
• WSDL: Web Services Description Language is
another XML based language that was created to
describe the usage of a particular web service, or
rather, how a particular service could be called.
• REST: Representation State Transfer, describes
of using the existing HTTP protocol to transmit
data.
4
Service Oriented Architecture
• Service-Oriented Architecture (SOA)
expresses a software architectural concept that
defines the use of services to support the
requirements of software users

5
Service Oriented Architecture

Service
Service Provider
Requestor Internet

Legacy
System
Service
Broker

6
Requirements for a SOA

• Interoperability between different systems 
and programming languages
• Clear and unambiguous description 
language
• Retrieval of the service
• Security

7
Web Services: Definition
W3C
• A Web service is a software system
– Identified by a URL, whose public interfaces and
bindings are defined and described using XML.
• Its definition can be discovered by other
software systems
• These systems may then interact with the Web
service
– using XML based messages conveyed by Internet
protocols
• Web Services implement a Service Oriented
Architecture 8
Web Services
• Web services are an emerging technology 
that offer a solution for providing a common 
collaborative  architecture. 
• Web services provide functional building 
blocks which are not tied to  any particular 
programming language or hardware 
platform. 
• They are accessible over standard Internet 
protocols.

9
The Evolution
• Online systems replace Batch systems
• Graphical User Interfaces replace “green”
screens
• Networked systems replace standalone systems
• Internet based systems replace LAN based
systems
• Future ???
– Web Services replace simple Internet and LAN
based systems

10
Technologies
• XML (eXtensible Markup Language)
– markup language that underlies most of the 
specifications used for Web services.  
• SOAP (Simple Object Access Protocol)
– (Simple Object Access Protocol), similar to JDBC, is a 
network, transport, and programming language and 
platform neutral protocol that allows a client to call a 
remote service. The message format is XML.
• WSDL (Web services description language)
– An XML­based interface and implementation description 
language. The service provider uses a WSDL document 
in order to specify the operations a Web service 
provides. 
• UDDI (universal description, discovery, and 
integration) 
– Both a client­side API and a SOAP­based server 
implementation that can be used to store and retrieve 
information on service providers and Web services.
11
How it works

• A Web Service is a URL-addressable software


resource that performs functions (or a function).

• Web Services communicate using standard


protocol known as SOAP (Simple Object Access
Protocol).

• A Web Service is located by its listing in a


Universal Discovery, Description and
Integration (UDDI) directory. 12
e Web Services good for?
Integration
• In a lot of organizations, the data and logic of
one application are basically useless to other
applications.
– When an application and its data are isolated
from other applications, we often say that they
are in “silos.”
• Web Services are better at sharing data and
functions.
– The result is that the “silos” come down, and
previously isolated systems can talk to each other.
13
hat are they good for?
Access
• Web Services are especially good at providing
access through different interfaces.
• A Web Service can have a dedicated client
application, but it can also be readily accessed
through browsers, wireless devices, voice-
activated interfaces, and so on.
• Adding new access methods is much simpler
than with a traditional application.

14
hat are they good for?
Flexibility
• One of the more important innovations in Web
Services is “machine-to-machine communications.”

• This means that a Web Service can ask another Web


Service to do something, and that Web Service can
ask another Web Service to do something, and so on.

• In the future, many Web Services will really just be


aggregations of other Web Services.

15
hat are they good for?
Application Assembly
• Future applications will be assembled from a
diverse group of web services, aggregating
functionality.

• Reusability is extended to the execution level,


versus reusability from just the design and
construction level.

16
SOAP
• Simple Object Access
Protocol
• SOAP is an open
protocol specification
defining a uniform way
of performing RPCs
using HTTP as the
underlying
communications protocol
with XML for the data
serialization.
17
SOAP
• The SOAP spec describes an intricate language
with numerous elements and attributes, intended
to describe most types of data.
• This information can be transported over any
number of protocols, but is most commonly sent
over HTTP along with other web traffic.
• There are two ways of using SOAP:
– Remote procedure call style
– Document Style

18
RPC-Style SOAP
• The RPC style of SOAP treats the web service
as though it were an object containing one or
more methods.
• A request is made to the service detailing the
method name to call and the parameters if any to
pass.
• The method is executed on the server and an
XML response is dispatched containing the
return value, if any.

19
RPC-Style SOAP: example
• A web service that provides simple arithmetic ops: addition,
subtraction, multiplication and division. Each method takes two
numbers and returns a result.
• An RPC style request for the add operation would look like this:
<?xml version = “1.0” encoding = “utf-8” ?>
<soap:Envelope xmlns:soap=
“http://schemas.xmlsoap.org/soap/envelope”
soap:encodingStyle=
“http://schemas.xmlsoap.org/soap/encoding/”>
<soap:Body>
<w:add xmlns:w= “http://www.wrox.com/services/math” >
<w:op1>4.5</w:op1>
<w:op2>5.4</w:op2>
</w:add>
</soap:Body>
</soap:Envelope> 20
RPC-style SOAP response
• If the request to add two numbers executed successfully, the
response message would look like:
<?xml version = “1.0” encoding = “utf-8” ?>
<soap:Envelope xmlns:soap=
“http://schemas.xmlsoap.org/soap/envelope”
soap:encodingStyle=
“http://schemas.xmlsoap.org/soap/encoding/”>
<soap:Body>
<w:addReponse xmlns:w= “http://www.wrox.com/services/math” >
<w:addResult>9.9</w:addResult>
</w:addResponse>
</soap:Body>
</soap:Envelope>
• If there were any problems processing the SOAP request on the
server, then a <soap:Fault> element will be returned.
21
Document-Style SOAP
• The document style of SOAP relies on XML schemas to
designate the format of the request and response.
• This style seems to be gaining in poularity and some predict that
it will eventually all but replace the RPC style.
• A document-style request may not look that different from an
RPC-style request. ( simply remove the soap:encodingStyle
attribute)
<?xml version = “1.0” encoding = “utf-8” ?>
<soap:Envelope xmlns:soap=
“http://schemas.xmlsoap.org/soap/envelope” >
<soap:Body>
<w:add xmlns:w= “http://www.wrox.com/services/math” op1=
“4.5” op2 = “5.4” />
</soap:Body>
</soap:Envelope> 22
WSDL
• Web Services Description Language
• WSDL is XML-based language
• Was created to describe the usage of a particular web
service, how a particular service could be called.
• The WSDL specification describes an incredibly dense
language, designed to be extremely flexible and allow
for as much re-use as possible.
• Typically, a software tool is used for the initial WSDL
file creation and then hand tweaked as necessary.

23
Difference between Document Style
and RPC style SOAP
• The Document Style sends the message as
elements within the <soap:body> that can have
whatever structure the sender and receiver agree
on using the embedded schema as a guide.
• An RPC-style message had an element named
after the method being called. This in turn will
have one element for each parameter the method
accepts.

24
REST ( Representation State Transfer)

• Describes a way of using the existing HTTP


protocol to transmit data.
• REST can be used for any type of HTTP based
request and response systems as well.
• With regard to web services, REST enables you
to call a given URL in a specific format to return
the data.

25
REST example
• Wrox would like to provide a way for others to retriev a list of all authors.
REST style web service use simple URLs to access data; the wrox book
service could use this URL to retrieve the list of authors:
• http://www.wrox.com/services/authors
• This service may return an XML representation of the known authors along
with information on how to access details about each one, such as:
<?xml version = “1.0” encoding = “utf-8” ?>
<authors xmlns:xlink= http://www/w3.org/1999/xlink
Xmlns = http://www.wrox.com/services/authors-books
Xlink:href=http://www.wrox.com/services/authors/>
<author forenames= “Michael” surname = “kay”
xlink:href= http://www.wrox.com/services/authors/kaym
id= “fawcettj” />
<!– More authors --- >
</authors>

26
REST example contd
• Xlink is a way of linking documents that goes way beyond the straightforward
hyperlinks in HTML. It provides capabilities to specify a two-way dependency so that
documents can be accessible from each other as well as indicating how a link should
be activated – for exmaple by hand, automatically or after a preset time.
• If used in a web site or web application, the XML returned from the REST service
would be transformed either client side or server-side to a more user-friendly format:
<html>
<head>
<title> Wrox Authors </title>
</head>
<body>
<a href=“ http://www.wrox.com/services/authors/kaym”>Michael Kay</a>
<a href=“ http://www.wrox.com/services/authors/fawcettj”>Joe Fawcett</a>
</body>
</html>

27
How Is a Service Described?
• WSDL (“Web Service Description Language”)
– XML-based
– Submitted to W3C for standardization

• Terraserver example
– http:terraserver.microsoft.com/default.aspx

28
How Do I Find Web Services?
• UDDI project by Microsoft, IBM, and others. Take a look at
//uddi.org

• Websites
– //www.xmethods.net
– //www.salcentral.com
• Commercial
– //www.serviceobjects.com
– //www.xmethods.net

29
ServiceObjects

30
FastWeather

31
Using the FastWeather Web Service

ServiceObjects
RV Web Service
Application Internet

32
WebSphere Studio Wizards
• Wizards make it easy for you to create and use
Web Services
– Wizard to create a Web Service from a JavaBean
• Generates WSDL document
• Creates all required software components
• Create a Test client
– Wizard to create a Web Service Client
• Use the WSDL document as input

33
Generate a Web Service Client

34
Client Configuration

35
Web Service Selection

36
Web Service Client Test

37
Testing

Input.jsp
Method.jsp

Result.jsp

38
Web Service HTML File

39
weather.html

<IFRAME src="Input.jsp?method=13" name=”input” scrolling="no"


frameborder="0" align="top" height="350"> </IFRAME>

<IFRAME src="Result.jsp" name="result" scrolling="no"


frameborder="0" align = “top” height="350">

40
weather.html

41
Web Service as part of RV Application

42
Lab Exercises
• Create Web Services client
• Integrate Web Service into the RV application

43

JSON
• JSON - JavaScript Object Notation
• Tutorial of this simple and universal data exchange format

• JSON is a part of the ECMAScript standard since ECMA has defined in 1999 the eval() function
that parses the format.
It has been popularized with the success of Ajax.
The JSON word appears often when one is speaking about Ajax. We know this is another data
format, that can replace XML, and this format is supported by a lot of programmers. But what is
exactly JSON, what are the advantages?
• Content
• Why JSON?
• JSON and XML
• The syntax of JSON
• Example of JSON file
• How to use the format
– Client side (browser)
– Server side
– Data exchange
– Example
• Simple demo
• YAML and JSON
• Resources

44
Why JSON
• Why JSON?
• The benefit of JSON is that it is recognized natively by JavaScript. No need
for parsing an XML document to extract the data and get it throught the net.
• JSON and XML
• Benefits of JSON:
- The easiness of reading.
- The easiness of using.
Benefits of XML:
- XML is extensible.
- It is widely used and recognized by almost all programming languages.
Unfortunally, both XML and JSON are enable to integrate a large amount of
data in binary form.

45
The syntax of JSON

• The components of JSON:


- An object: contains objets or attributes.
- A scalar variable: Number, String, Boolean.
- An array.
- Literal values: null, true, false, "string of characters", and numerical values.
• Object
• It contains a member or a list of members, and each member has the form:
• "name" : "value" The syntax of the object is:
• { member, member, .... } Array
• A collection of values, separated by commas. [ value, value, ....] Values
• A value may be: an object, an array, a litteral (string, number, true, false,
null).
• Nothing more is required to create a JSON file!

46
Example
• {
"menu": "File",
"commands": [
{
"title": "New",
"action":"CreateDoc"
},
{
"title": "Open",
"action": "OpenDoc"
},
{
"title": "Close",
"action": "CloseDoc"
}
]
}

47
XML Equivalent
• <?xml version="1.0" ?>
<root>
<menu>File</menu>
<commands>
<item>
<title>New</value>
<action>CreateDoc</action>
</item>
<item>
<title>Open</value>
<action>OpenDoc</action>
</item>
<item>
<title>Close</value>
<action>CloseDoc</action>
</item>
</commands>
</root>

48
How to use the format

The JSON file allows to load data from the server or to send data to it, in this format. For example,
storing the content of a form, just filled by an user. This involves three steps: the browser
processing, the server processing, and the data exchange between them.
Client side (browser)
This is rather easy, as JSON is a part of the JavaScript definition. The content of a file, or the
definition of the data is assigned to a variable, and this variable becomes an object of the program.
Server side
JSON file are used by various programming languages, including PHP and Java thanks to parsers that
allow to get the content and that may even convert it into classes and attributes of the language.
The json.org includes a C parser and a list of parsers in other languages.
Data exchange
Loading a file may be accomplished from JavaScript in several ways:
- direct including of the file into the HTML page, as a JavaScript .js external file.
- loading by a JavaScript command.
- using XMLHttpRequest.
The JSON file is parsed by the eval() JavaScript function.
Sending the file to the server may be accomplished by XMLHttpRequest. The file is sent as a text
file and processed by the parser of the programming language that uses it.

49
Example

• The XMLHttpRequest code:


• var req = new XMLHttpRequest();
• req.open("GET", "file.json", true);
• req.onreadystatechange = myCode; // the handler
• req.send(null);

• The JavaScript handler:


function myCode() {
if (req.readyState == 4)
{ var doc = eval('(' + req.responseText + ')'); }
}

• Using the data:


var menuName = document.getElementById('jsmenu'); // finding a field menu
Name.value = doc.menu.value; // assigning a value to the field

How to access data:


doc.commands[0].title // read value of the "title" field in the array
doc.commands[0].action // read value of the "action" field in the array

50

You might also like