Professional Documents
Culture Documents
AJAX: Ajax Client Server Architecture-XML Http Request Object-Call Back Methods; Web
Services: Introduction- Java web services Basics – Creating, Publishing, Testing and Describing a
Web services (WSDL)-Consuming a web service, Database Driven web service from an web
application – SOAP.
Introduction to AJAX:
AJAX is an acronym for Asynchronous JavaScript And XML. AJAX is a new technique for
creating better, faster and interactive web applications with the help of JavaScript, DOM, XML, HTML,
CSS etc. AJAX allows you to send and receive data asynchronously without reloading the entire web page.
So, it is fast. AJAX allows you to send only important information to the server not the entire page. So
only valuable data from the client side is routed to the server side. It makes your application interactive and
faster. Ajax is the most viable Rich Internet Application (RIA) technique so far.
AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to
be updated asynchronously by exchanging small amounts of data with the server behind the
scenes. This means that it is possible to update parts of a web page, without reloading the whole
page.
Where it is used?
There are too many web applications running on the web that are using AJAX Technology. Some
are:
1. Gmail
2. Face book
3. Twitter
4. Google maps
5. YouTube etc.,
AJAX stands for Asynchronous Java Script and XML. It allows the various client requests
at the server, the server should process the required processing, and automatically loads the page
without refreshing the page i.e., without user need to move from one page to other page.
Asynchronous means that the script will send a request to the server, and continue it's
execution without waiting for the reply. As soon as reply is received a browser event is fired,
which in turn allows the script to execute associated actions.
Synchronous Vs. Asynchronous Application Before understanding AJAX, let’s understand classic
web application model and AJAX Web application model.
Synchronous (Classic Web-Application Model) A synchronous request blocks the client until
As you can see in the above image, full page is refreshed at request time and user is
blocked until request completes.
1. User sends a request from the UI and a JavaScript call goes to XMLHttpRequest object.
2. HTTP Request is sent to the server by XMLHttpRequest object.
3. Server interacts with the database using JSP, PHP, Servlet, ASP.net etc.
4. Data is retrieved.
5. Server sends XML data or JSON data to the XMLHttpRequest callback function.
6. HTML and CSS data is displayed on the browser.
3. Server interacts with the database using JSP, PHP, Servlet, ASP.net etc.
4. Data is retrieved.
5. Server sends XML data or JSON data to the XMLHttpRequest callback function.
Method Description
XMLHttpRequest Properties
In order to specify the type of data that your Ajax applications expect to receive from the
web server and to monitor and respond to that data once it has been received from the web server,
you need to work with different XMLHttpRequest properties.
The following statement demonstrates how to set up your Ajax application to handle text data
returned by the web server.
RequestObj.innerHTML = Request.responseText;
Alternatively, you can set up your application to process XML data, as demonstrated here:
RequestObj.innerHTML = Request.responseXML;
Example:
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc('ajax_info.txt', myFunction)"> Change
Content </button>
</div>
<script>
function loadDoc(url, cFunction) {
var xhttp;
xhttp=new XMLHttpRequest ();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
cFunction(this);
}
};
xhttp.open("GET", url, true);
xhttp.send();
The value 0 indicates that the request is not initialized and the value 4 indicates that the
request is complete. If the request completes successfully use the XMLHttpRequest object’s
responseText property to obtain the response data and place it in the div element named
contentArea.
Example:
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc('ajax_info.txt', myFunction)"> Change
Content </button>
</div>
<script>
function loadDoc(url, cFunction) {
var xhttp;
xhttp=new XMLHttpRequest ();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
cFunction(this);
}
Advantages:
The working diagram of a Web Service is defined by the three components shown below.
Service Requestor (Find())
Service Provider (Publish())
Service Registry or Repository (Bind())
The first phase is the Publish() phase where a Service Provider feeds all the details about a
Web Service in a Service Registry or Repository.
The second phase is Find() where a Service Request mainly the client application finds the
details about Web Service from a repository (also has WSDL XML file).
Java webservices
A web service is a software component stored on one computer that can be accessed via
method calls by an application (or other software component) on another computer over a
network. Web services communicate using such technologies as XML and HTTP. Several Java
APIs facilitate web services. In this chapter, we’ll be dealing with APIs that are based on the
Simple Object Access Protocol (SOAP)—an XML-based protocol that allows web services and
clients to communicate, even if the client and the web service are written in different languages.
There are other web services technologies, such as Representational State Transfer (REST).
SOAP: SOAP acronym for Simple Object Access Protocol. It defines the standard XML
format. It also defines the way of building web services. We use Web Service Description
Language (WSDL) to define the format of request XML and the response XML.
A web service is a software component stored on one computer that can be accessed via
method calls by an application (or other software component) on another computer over a
network. Web services communicate using such technologies as XML and HTTP. Several Java
APIs facilitate web services. In this chapter, we’ll be dealing with APIs that are based on the
Simple Object Access Protocol (SOAP)—an XML-based protocol that allows web services and
clients to communicate, even if the client and the web service are written in different languages.
There are other web services technologies, such as Representational State Transfer (REST).
1. Creating a Web Application Project and Adding a Web Service Class in NetBeans
Creating a Web Application Project in Netbeans 5.5
To create a web application, perform the following steps:
1. Select File > New Project to open the New Project dialog.
2. Select Web from the dialog’s Categories list, then select Web Application from the Projects list.
Click Next >.
3. Specify the name of your project (HugeInteger) in the Project Name field and specify where
you’d like to store the project in the Project Location field. You can click the Browse button to
select the location.
4. Select Sun Java System Application Server from the Server drop-down list.
5. Select Java EE 5 from the J2EE Version drop-down list.
6. Click Finish to dismiss the New Project dialog
This creates a web application that will run in a web browser. When you create a Web Application
in Netbeans, the IDE generates additional files that support the web application
Adding a Web Service Class to a Web Application Project
To create a web service, perform the following steps to add a web service class to the project:
1. Under the Projects tab in Netbeans (just below the File menu), right click the HugeInteger
project’s node and select New > Web Service… to open the New Web Service dialog.
2. Specify HugeInteger in the Web Service Name field.
3. Specify com.deitel.jhtp7.ch28.hugeinteger in the Package field.
4. Click Finish to dismiss the New Web Service dialog
package com.deitel.jhtp7.ch28.hugeinteger;
4. Testing the HugeInteger Web Service with Sun Java System Application Server’s Tester
Web page
Sun Java System Application Server
– Can dynamically create a Tester web page for testing a web service’s methods from a web
browser
– Enable this feature via the project’s Run options
To display the Tester web page
– Run the web application from NetBeans, or
– Type web service’s URL in browser’s address field followed by ?Tester
Web server must be running for a client to access a web service
– If NetBeans launches the application server for you, the server will shut down when you
close Netbeans
– To keep it running, launch it independently of Netbeans
– Client tools parse the WSDL to create the client-side proxy class that accesses the web
service
To view the WSDL for a web service
– Type URL in the browser’s address field followed by ?WSDL or
– Click the WSDL File link in the Sun Java System Application Server’s Tester web page
</SOAP_ENV:Envelope>
SOAP Response
contains an element called "TutorialID" which is of the type string. Similarly, the
TutorialResponse operation contains an element called "TutorialName" which is also a
type string.
2. <portType> - This actually describes the operation which can be performed by the web
service, which in our case is called Tutorial. This operation can take 2 messages; one is
an input message, and the other is the output message.
3. <binding> - This element contains the protocol which is used. So in our case, we are
defining it to use http (http://schemas.xmlsoap.org/soap/http). We also specify other
details for the body of the operation, like the namespace and whether the message should
be encoded.
Step 2: Right Click on the DatabasesSelect New Connection as shown below in Fig 2.
Step 4: Click on the Ok button Now expand the Newly created database connection.
Step 5: Create a table named login-->Right Click on the Tables and select Create table as shown
below in Fig 5
Step 6:
It opens a dialog box for giving the fields name of the table
Now give the field name and data type as shown below in Fig 6.
Example:
package mypack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.sql.DataSource;
@WebService()
public class myaccount {
@WebMethod(operationName = "insert")
public String insert(@WebParam(name = "uname") String uname, @WebParam(name = "fname")
String fname,
@WebParam(name = "lname") String lname, @WebParam(name = "location") String location,
Step:
• Now deploy the project
• Right click on the project
• Select Undeploy and Deploy.
• It builds and deploys the MyAccount Web project on the glassfish server.
• After deploying now we can test the Web Service created
• Right click on the myaccount webservice
• Select Test Web Service as shown below in Fig.
AJAX, which stands for asynchronous JavaScript and XML, is a technique that allows
web pages to be updated asynchronously, which means that the browser doesn't need to reload the
entire page when only a small bit of data on the page has changed. AJAX passes only the updated
information to and from the server.