section is used to display information from a server.
The calls a function (if it is clicked).
The function requests data from a web server and displays it
1
Web Technology UNIT-5
AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. How AJAX Works
1. An event occurs in a web page (the page is loaded, a button is clicked) 2. An XMLHttpRequest object is created by JavaScript 2
Web Technology UNIT-5
3. The XMLHttpRequest object sends a request to a web server 4. The server processes the request 5. The server sends a response back to the web page 6. The response is read by JavaScript 7. Proper action (like page update) is performed by JavaScript
TECHNOLOGIES USED IN AJAX:
AJAX cannot work independently. It is used in combination with other technologies to create interactive WebPages. JavaScript
Loosely typed scripting language. JavaScript function is called when an event occurs in a page. Glue for the whole AJAX operation. DOM
API for accessing and manipulating structured documents. Represents the structure of XML and HTML documents. CSS
Allows for a clear separation of the presentation style from the content and may be changed programmatically by JavaScript
AJAX CLIENT SERVER ARCHITECTURE
3
Web Technology UNIT-5
The XMLHttpRequest Object
The keystone of AJAX is the XMLHttpRequest object.
All modern browsers support the XMLHttpRequest object.
The XMLHttpRequest object can be used to exchange data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
The XMLHttpRequest object is the key to AJAX. It has been available ever since Internet Explorer 5.5 was released in July 2000, but was not fully discovered until AJAX and Web 2.0 in 2005 became popular.
XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript, and other web browser scripting languages to transfer and manipulate XML data to and from a webserver using HTTP, establishing an independent connection channel between a webpage's Client-Side and Server-Side.
The data returned from XMLHttpRequest calls will often be provided by back-end databases. Besides XML, XMLHttpRequest can be used to fetch data in other formats, e.g. JSON or even plain text . Create an XMLHttpRequest Object
All modern browsers (Chrome, Firefox, IE7+, Edge, Safari , Opera ) have a built-in XMLHttpRequest object.
Syntax for creating an XMLHttpRequest object:
4
Web Technology UNIT-5
XMLHttpRequest Methods
abort()
Cancels the current request.
getAllResponseHeaders()
Returns the complete set of HTTP headers as a string.
getResponseHeader( headerName )
Returns the value of the specified HTTP header.
open( method, URL ) open( method, URL, async ) open( method, URL, async, userName ) open( method, URL, async, userName, password )
Specifies the method, URL, and other optional attributes of a request.
The method parameter can have a value of "GET", "POST", or "HEAD". Other HTTP methods such as "PUT" and "DELETE" (primarily used in REST applications) may be possible.
The "async" parameter specifies whether the request should be handled asynchronously or not. "true" means that the script processing carries on after the send() method without waiting for a response, and "false" means that the script waits for a response before continuing script processing.
send( content )
Sends the request.
setRequestHeader( label, value )
Adds a label/value pair to the HTTP header to be sent.
5
Web Technology UNIT-5
6
Web Technology UNIT-5
7
Web Technology UNIT-5
JQuery Callback Functions
JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors.
To prevent this, you can create a callback function.
A callback function is executed after the current effect is finished.
Typical syntax: $(selector).hide(speed,callback);
Examples
The example below has a callback parameter that is a function that will be executed after the hide effect is completed:
The example below has no callback parameter, and the alert box will be displayed before the hide effect is completed:
8
Web Technology UNIT-5
Web Services
Web services are open standard (XML, SOAP, HTTP, etc.) based web applications that interact with other web applications for the purpose of exchanging data. Web services can convert your existing applications into web applications. In this tutorial, you will learn what exactly web services are and why and how to use them.
A web service is any piece of software that makes itself available over the internet and uses a standardized XML messaging system. XML is used to encode all communications to a web service. For example, a client invokes a web service by sending an XML message, then waits for a corresponding XML response. As all communication is in XML, web services are not tied to any one operating system or programming language-- Java can talk with Perl; Windows applications can talk with Unix applications. Web services are self-contained, modular, distributed, dynamic applications that can be described, published, located, or invoked over the network to create products, processes, and supply chains. These applications can be local, distributed, or web-based. Web services are built on top of open standards such as TCP/IP, HTTP, Java, HTML, and XML. Web services are XML-based information exchange systems that use the Internet for direct application-to-application interaction. These systems can include programs, objects, messages, or documents. A web service is a collection of open protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computeris . This interoperability (e.g., between Java and Python, or Windows and Linux applications) due to the use of open standards.
9
Web Technology UNIT-5
Components of Web Services
The basic web services platform is XML + HTTP. All the standard web services work using the following components
SOAP (Simple Object Access Protocol) UDDI (Universal Description, Discovery and Integration) WSDL (Web Services Description Language) SOAP
SOAP is an acronym for Simple Object Access Protocol. SOAP is a XML-based protocol for accessing web services. SOAP is a W3C recommendation for communication between applications. SOAP is XML based, so it is platform independent and language independent. In other words, it can be used with Java, .Net or PHP language on any platform.
WSDL
WSDL is an acronym for Web Services Description Language. WSDL is a xml document containing information about web services such as method name, method parameter and how to access it. WSDL is a part of UDDI. It acts as a interface between web service applications. WSDL is pronounced as wiz-dull.
10
Web Technology UNIT-5
UDDI
UDDI is an acronym for Universal Description, Discovery and Integration. UDDI is a XML based framework for describing, discovering and integrating web services. UDDI is a directory of web service interfaces described by WSDL, containing information about web services. Web Service Roles
There are three major roles within the web service architecture −
Service Provider
This is the provider of the web service. The service provider implements the service and makes it available on the Internet.
Service Requestor
This is any consumer of the web service. The requestor utilizes an existing web service by opening a network connection and sending an XML request.
Service Registry
This is a logically centralized directory of services. The registry provides a central place where developers can publish new services or find existing ones. It therefore serves as a centralized clearing house for companies and their services.
WEB SERVICE COMMUNICATION MODELS:
RPC-Based Communication Model
The RPC-based communication model defines a request/response-based synchronous communication. When the client sends a request, the client waits until a response is sent back from the server before continuing any operation. Typical to implementing CORBA or RMI communication, the RPC-based Web services are tightly coupled and are implemented with remote objects to the client application.
11
Web Technology UNIT-5
Messaging-Based Communication Model
The messaging-based communication model defines a loosely coupled and document-driven communication. The service requestor invoking a messaging-based service provider does not wait for a response. How Does a Web Service Work?
A web service enables communication among various applications by using open standards such as HTML, XML, WSDL, and SOAP. A web service takes the help of −
XML to tag the data SOAP to transfer a message WSDL to describe the availability of service.
You can build a Java-based web service on Solaris that is accessible from your Visual Basic program that runs on Windows.
You can also use C# to build new web services on Windows that can be invoked from your web application that is based on Java Server Pages (JSP) and runs on Linux.
Client (e.g. mobile app) sends a request to the server with response data, e.g. name of the city to find current weather for (data could be in any format like JSON or XML etc.). Server processes the request according to the requested data (name of the city). In another word, it fetches the current weather details from the database. Then sends the data back to the client (mobile app) and client shows the response data in proper format.
12
Web Technology UNIT-5
Java Web Services
JAX-WS and JAX-RS. The java web service application can be accessed by other programming languages such as .Net and PHP.
Java web service application perform communication through WSDL (Web Services Description Language). There are two ways to write java web service application code: SOAP and RESTful. Java Web Services API
There are two main API's defined by Java for developing web service applications since JavaEE6.
1) JAX-WS: for SOAP web services. The are two ways to write JAX-WS application code: by RPC style and Document style.
2) JAX-RS: for RESTful web services. There are mainly 2 implementation currently in use for creating JAX-RS application: Jersey and REST easy.
Both of these APIs are part of standard JDK installation, so we don’t need to add any jars to work with them. Both of these APIs use annotations very heavily.
13
Web Technology UNIT-5
JAX-WS
JAX-WS tutorial is provides concepts and examples of JAX-WS API. This JAX-WS tutorial is designed for beginners and professionals.
There are two ways to develop JAX-WS example.
1. RPC style 2. Document style
\
JAX-WS Example RPC Style
Creating JAX-WS example is a easy task because it requires no extra configuration settings.
JAX-WS API is inbuilt in JDK, so you don't need to load any extra jar file for it. Let's see a simple example of JAX-WS example in RPC style.
There are created 4 files for hello world JAX-WS example:
1. HelloWorld.java 2. HelloWorldImpl.java 3. Publisher.java 4. HelloWorldClient.java
The first 3 files are created for server side and 1 application for client side.
14
Web Technology UNIT-5
JAX-WS Server Code
File: HelloWorld.java
File: HelloWorldImpl.java
15
Web Technology UNIT-5
JAX-WS Client Code
File: HelloWorldClient.java
Output:
JAX-RS
JAX-RS tutorial is provides concepts and examples of JAX-RS API. This JAX-RS tutorial is designed for beginners and professionals.
There are two main implementation of JAX-RS API.
1. Jersey 2. RESTEasy
16
Web Technology UNIT-5
JAX-RS Example Jersey
We can create JAX-RS example by jersey implementation. To do so, you need to load jersey jar files or use maven framework.
In this example, we are using jersey jar files for using jersey example for JAX-RS.
There are created 4 files for hello world JAX-RS example:
1. Hello.java 2. web.xml 3. index.html 4. HelloWorldClient.java
The first 3 files are created for server side and 1 application for client side.
17
Web Technology UNIT-5
JAX-RS Server Code
File: Hello.java
18
Web Technology UNIT-5
File: web.xml
File: index.html
19
Web Technology UNIT-5
Now run this application on server. Here we are using Tomcat server on port 4444. The project name is restfuljersey.
After running the project, you will see the following outpu
JAX-RS Client Code
The ClientTest.java file is created inside the server application. But you can run client code by other application also by having service interface and jersey jar file.
File: ClientTest.java
20
Web Technology UNIT-5
Output
SOAP
SOAP Web Services
SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing web services. 21
Web Technology UNIT-5
SOAP is a W3C recommendation for communication between two applications.
SOAP is XML based protocol. It is platform independent and language independent. By using SOAP, you will be able to interact with other programming language applications. Advantages of Soap Web Services
WS Security: SOAP defines its own security known as WS Security.
Language and Platform independent: SOAP web services can be written in any programming language and executed in any platform. Disadvantages of Soap Web Services
Slow: SOAP uses XML format that must be parsed to be read. It defines many standards that must be followed while developing the SOAP applications. So it is slow and consumes more bandwidth and resource.
WSDL dependent: SOAP uses WSDL and doesn't have any other mechanism to discover the service.
IMPORTANT POINTS
SOAP stands for Simple Object Access Protocol SOAP is an application communication protocol SOAP is a format for sending and receiving messages SOAP is platform independent SOAP is based on XML SOAP is a W3C recommendation
Why SOAP?
It is important for web applications to be able to communicate over the Internet.
The best way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this.
22
Web Technology UNIT-5
SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages. SOAP Building Blocks
A SOAP message is an ordinary XML document containing the following elements:
An Envelope element that identifies the XML document as a SOAP message A Header element that contains header information A Body element that contains call and response information A Fault element containing errors and status information Syntax Rules
Here are some important syntax rules:
A SOAP message MUST be encoded using XML A SOAP message MUST use the SOAP Envelope namespace A SOAP message MUST use the SOAP Encoding namespace A SOAP message must NOT contain a DTD reference A SOAP message must NOT contain XML Processing Instructions
Skeleton SOAP Message
23
Web Technology UNIT-5
The SOAP Envelope Element
The required SOAP Envelope element is the root element of a SOAP message. This element defines the XML document as a SOAP message.
24
Web Technology UNIT-5
The encoding Style Attribute
The encoding Style attribute is used to define the data types used in the document. This attribute may appear on any SOAP element, and applies to the element's contents and all child elements.
A SOAP message has no default encoding.
Syntax
25
Web Technology UNIT-5
The SOAP Header Element
The optional SOAP Header element contains application-specific information (like authentication, payment, etc) about the SOAP message.
If the Header element is present, it must be the first child element of the Envelope element.
Note: All immediate child elements of the Header element must be namespace-qualified.
The example above contains a header with a "Trans" element, a "must Understand" attribute with a value of 1, and a value of 234.
SOAP defines three attributes in the default namespace. These attributes are: must Understand, actor, and encoding Style.
The attributes defined in the SOAP Header defines how a recipient should process the SOAP message.
26
Web Technology UNIT-5
THE MUST Understand Attribute
The SOAP must understand attribute can be used to indicate whether a header entry is mandatory or optional for the recipient to process.
If you add must Understand="1" to a child element of the Header element it indicates that the receiver processing the Header must recognize the element. If the receiver does not recognize the element it will fail when processing the Header.
Syntax
The SOAP Body Element
The required SOAP Body element contains the actual SOAP message intended for the ultimate endpoint of the message.
Immediate child elements of the SOAP Body element may be namespace-qualified.
Example
27
Web Technology UNIT-5
The example above requests the price of apples. Note that the m:GetPrice and the Item elements above are application-specific elements. They are not a part of the SOAP namespace.
A SOAP response could look something like this:
The SOAP Fault Element
The optional SOAP Fault element is used to indicate error messages.
The SOAP Fault element holds errors and status information for a SOAP message.
If a Fault element is present, it must appear as a child element of the Body element. A Fault element can only appear once in a SOAP message.
The SOAP Fault element has the following sub elements:
28
Web Technology UNIT-5
SOAP Fault Codes
The fault code values defined below must be used in the fault code element when describing faults:
29
Web Technology UNIT-5
SOAP Binding
The SOAP specification defines the structure of the SOAP messages, not how they are exchanged. This gap is filled by what is called "SOAP Bindings". SOAP bindings are mechanisms which allow SOAP messages to be effectively exchanged using a transport protocol.
Most SOAP implementations provide bindings for common transport protocols, such as HTTP or SMTP.
HTTP is synchronous and widely used. A SOAP HTTP request specifies at least two HTTP headers: Content-Type and Content-Length.
SMTP is asynchronous and is used in last resort or particular cases.
Java implementations of SOAP usually provide a specific binding for the JMS (Java Messaging System) protocol.
30