Ajax
Ajax
Ajax, asynchronous JavaScript and XML, is a Web application development method that uses client script to exchange data with a Web server . this way, Web pages can be updated dynamically without interrupting the interactive process . with Ajax, you can create a direct, highly available, richer, more dynamic Web user interface that is close to local desktop applications .
AJAX Processing Process
An AJAX interaction begins with a JavaScript object called XMLHttpRequest . as the name implies, it allows a client script to execute the HTTP request and will parse an XML-formatted server response . The first step in the AJAX process is to create a XMLHttpRequest instance . Use the HTTP method (get or post) to process the request and set the target URL to the XMLHttpRequest object .
Now, remember how Ajax is first in the asynchronous processing state? When you send an HTTP request, you do not want the browser to hang up and wait for the server to respond, instead, you want to continue to interact with the user's interface through the page and process them after the server response has actually arrived . to do this, you can register a callback function with XMLHttpRequest and distribute XMLHttpRequest requests asynchronously . control is immediately returned to the browser, and the callback function is invoked when the server response arrives .
On a Java Web server , the request to arrive is the same as any other httpservletrequest . after parsing the request parameter, theservlet executes the required application logic, serializes the response into the XML, and writes it back to HttpServletResponse.
An AJAX interaction begins with a JavaScript object called XMLHttpRequest . as the name implies, it allows a client script to execute the HTTP request and will parse an XML-formatted server response . The first step in the AJAX process is to create a XMLHttpRequest instance . Use the HTTP method (get or post) to process the request and set the target URL to the XMLHttpRequest object .
Now, remember how Ajax is first in the asynchronous processing state? When you send an HTTP request, you do not want the browser to hang up and wait for the server to respond, instead, you want to continue to interact with the user's interface through the page and process them after the server response has actually arrived . to do this, you can register a callback function with XMLHttpRequest and distribute XMLHttpRequest requests asynchronously . control is immediately returned to the browser, and the callback function is invoked when the server response arrives .
On a Java Web server , the request to arrive is the same as any other httpservletrequest . after parsing the request parameter, theservlet executes the required application logic, serializes the response into the XML, and writes it back to HttpServletResponse.
An AJAX interaction begins with a JavaScript object called XMLHttpRequest . as the name implies, it allows a client script to execute the HTTP request and will parse an XML-formatted server response . The first step in the AJAX process is to create a XMLHttpRequest instance . Use the HTTP method (get or post) to process the request and set the target URL to the XMLHttpRequest object .
Now, remember how Ajax is first in the asynchronous processing state? When you send an HTTP request, you do not want the browser to hang up and wait for the server to respond, instead, you want to continue to interact with the user's interface through the page and process them after the server response has actually arrived . to do this, you can register a callback function with XMLHttpRequest and distribute XMLHttpRequest requests asynchronously . control is immediately returned to the browser, and the callback function is invoked when the server response arrives .
On a Java Web server , the request to arrive is the same as any other httpservletrequest . after parsing the request parameter, theservlet executes the required application logic, serializes the response into the XML, and writes it back to HttpServletResponse.
Instance
Here's a simple example of what I've written:
Ajaxservlet.java Program list:
Package com;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/*
*------------------------------------------------------
* FileName: Ajaxservlet.java
* Date Created: 2005-10-25
* Original Author: Miaohai
* All rights reserved: Miaohai
* Function Description: Generate XML document
*
* Change record: N/A
*------------------------------------------------------
*/
public class Ajaxservlet
Extends HttpServlet
{
private static final String content_type= "Text/xml; charset=gb2312 ";
public void Init ()
Throws Servletexception
{
}
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception,ioexception
{
Response.setcontenttype (Content_Type);
PrintWriter Out=response.getwriter ();
String action=request.getparameter ("action");
if (("Send"). Equals (Action)
{
StringBuffer sb=new stringbuffer ("<type>");
Sb.append ("<type_name>AA</type_name>");
Sb.append ("<type_name>BB</type_name>");
Sb.append ("<type_name>CC</type_name>");
Sb.append ("<type_name>DD</type_name>");
Sb.append ("</type>");
Out.write (Sb.tostring ());
Out.close ();
}
}
}
HTML page:
<title>AJAX.html</title>
<script type= "Text/javascript" >
/* Send parameters via asynchronous transfer XMLHTTP to Ajaxservlet, return the eligible XML document * *
function GetResult ()
{
var url = "Servlet/com.ajaxservlet?action=send";
if (window. XMLHttpRequest)
{
req = new XMLHttpRequest ();
}else if (window. ActiveXObject)
{
req = new ActiveXObject ("Microsoft.XMLHTTP");
}
if (req)
{
Req.open ("Get", url, True);
Req.onreadystatechange = complete;
Req.send (NULL);
}
}
/* Parse the returned XML document * *
function complete ()
{
if (req.readystate = 4)
{
if (Req.status = 200)
{
var type = Req.responseXML.getElementsByTagName ("type_name");
var str=new Array ();
for (Var i=0;i<type.length;i++)
{
Str[i]=type[i].firstchild.data;
document.all[' TD '].innerhtml+=str[i]+ "<BR>";
}
}
}
}
</script>
<body >
<table width= "80%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
<TD id= "TD" > </td>
</tr>
</table>
</body>