Web-Services
Objectives of the session
• Introduction to Webservices
• Related Technologies
• Soap
– RPC-Style SOAP
– Document-Style SOAP
• WSDL
• REST
WebServices
• Web services provide a way to
exchange data between applications and
servers.
• To facilitate this communication, web
services use the Internet to send
messages composed of XML data back
and forth between a consumer (the
application that uses the data) and a
provider (the server that contains the
data).
• 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 same task.
WebServices
SOAP
• SOAP is a combination of an XML-based language and any
number of common protocols for transmitting this data.
• Originally an acronym for Simple Object Access Protocol,
the specification now simply goes by SOAP.
• Information can be transported over any number of
protocols, but is most commonly sent over HTTP along
with other web traffic.
• Universal Description, Discovery and Integration
(UDDI) is a directory service where businesses
can register and search for Web services.
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, or an error message if something
went awry.
SOAP – RPC Style Example
<?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:addResponse xmlns:w=" http://www.wrox.com
/services/math">
<w:addResult>9.9</w:addResult>
</w:addResponse>
</soap:Body>
</soap:Envelope>
SOAP – Document Syle
• The document style has flexibility because
there is an accompanying XML schema.
• A web service can use this XML schema to
validate the structure of the request; the
service is then free to use the information in the
request appropriately.
• Responses follow the same basic rules as
requests: they can be very similar to RPC style
or completely different, again based on an
XML schema.
SOAP – Document Syle - Example
<?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>
WSDL
• Web Services Description Language (WSDL) 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.
• an XML-based language for describing network services
• WSDL has descriptions of capabilities and locations of
services
• like an interface description language for Web services
• communication using SOAP or direct HTTP
REST
• Representational State Transfer, often abbreviated as REST,
describes a way of using the existing HTTP protocol to transmit
data.
• Although used mostly for web services, REST can be used for any
type of HTTP-based request and response systems as well.
• In regard to web services, REST enables you to call a given URL
in a specific format to return data
• (which will also be in a specific format).
• This data may contain further information on how to retrieve even
more data.
• For the web service usage, the data will be returned as XML.
Example of a REST Service using Yahoo
Images Search WebService
Sample Code
Running Code
RECAP
• Webservices
• SOAP
• WSDL
• REST
JSON
• JSON is a very lightweight data format based on a
subset of the JavaScript syntax, namely array and
object literals.
• Because it uses JavaScript syntax, JSON definitions
can be included within JavaScript files and accessed
without the extra parsing that comes along with
XML-based languages.
• JAVASCRIPT Object Notation
Array Literals
• array literals are specified by using square brackets
([ and ]) to enclose a comma-delimited list of
JavaScript values (meaning a string, number,
Boolean, or null value)
– var aNames = ["Benjamin", "Michael", "Scott"];
– alert(aNames[0]); //outputs "Benjamin" alert(aNames[1]);
//outputs "Michael" alert(aNames[2]); //outputs "Scott"
• arrays in JavaScript are not typed,
they can be used to store any number
of different data types:
– var aValues = ["string", 24, true,
null];
Object Literals
• Object Literals are used to store information in name-
value pairs.
• An object literal is defined by two curly braces ( { } )
inside which any number of name-value pairs can be
placed.
– var oCar = {
“color” : “red”,
“doors” : ,
“paidFor” : true
};
alert(oCar.color); //outputs “red”
alert(oCar.doors); //outputs “4”
alert(oCar.paidFor); //outputs “true”
Mixing Literals
• It is possible to mix object and array literals creating an array of
objects or an object containing an array.
var aCars = [
{
“color”:”red”,
“doors”:2,
“paidFor”:true
},
{
“color”:”red”,
“doors”:2,
“paidFor”:true
},
{
“color”:”red”,
“doors”:2,
“paidFor”:true
}
];
Mixing Literals
• Alert (aCars[1].doors); //outputs “4”
JSON Syntax
• JSON Syntax is the mixture of object and
array literals to store data.
{
“availableColors” : [ “red”, “white”, “blue”],
“availableDoors”: [2, 4]
}
Var oCarInfo = eval (“(“ + sJSON + “)”);
Alert (oCarInfo.availableColors[0]); //outputs “red”
Alert (oCarInfo.availableDoors[1]); //outputs “4”
JSON Encoding and Decoding
• Inventor of JSON Crockford has created a utility to
decode and encode between JSON and JavaScript
objects.
• http://www.json.org/json.js
– Var oObject = JSON.parse(sJSON);
– JSON.stringify() method will conver JavaScript
object to JSON string.
JSON versus XML
• One of the advantages of JSON over XML is that it's more compact. XML is considered by
some to be overly verbose for its purpose.
<classinfo>
<students> <student>
<name>Michael Smith</name>
<average>99.5</average>
<age>17</age>
<graduating>true</graduating>
</student>
<student>
<name>Steve Johnson</name>
<average>34.87</average>
<age>17</age>
<graduating>false</graduating>
</student>
<student>
<name>Rebecca Young</name>
<average>89.6</average>
<age>18</age>
<graduating>true</graduating>
</student>
</students>
</classinfo>
JSON Vs XML
• { "classinfo" : { "students" : [ { "name" : "Michael
Smith", "average" : 99.5, "age" : 17, "graduating" : true },
{ "name" : "Steve Johnson", "average" : 34.87, "age" : 17,
"graduating" : false }, { "name" : "Rebecca Young",
"average" : 89.6, "age" : 18, "graduating" : true } ] } }
• Not including spaces JSON data is 224 bytes
compared to XML data of 365 bytes.
• JSON format is less readable when compared
to XML
RECAP
JSON
Array Literal
Object Literal
JSON vs. XML
JSON Examples
JSON Instances
JSON Exercises
Thank You