Ajax simple examples
Abstract: simple examples of Ajax
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="219" height="86" align="center" valign="top"> </ td> <td width="465" valign="top">
Ajax, for Asynchronous JavaScript and XML is the use of client-side script and the Web server data exchange Web application development methods. In this way, interactive Web pages do not have to interrupt the process of re-plus CD, can be dynamically updated. Using Ajax, you can create closer to a desktop application, direct, high availability, and even richer, more dynamic Web user interface.
Ajax process
An interactive Ajax from a group called XMLHttpRequest JavaScript objects began. As the name suggests, it allows a client-side scripting to implement HTTP requests, and an XML format will be analytic server response. Ajax the first step in the process is to create a XMLHttpRequest examples. Use HTTP methods (GET or POST) to deal with the request, and will target URL to the XMLHttpRequest object.
Now, remember how Ajax in the first asynchronous processing state? When you send HTTP requests, you do not want to wait for the browser and server hung response Instead, the pages you wish to continue to respond to users through the interactive interface and the server response to the arrival of the real deal with them. To complete it, you can XMLHttpRequest register a callback function, and asynchronous request to distribute XMLHttpRequest. Control immediately be returned to the browser, when the server response arrived, the callback function will be called.
- In the Java Web server, at the request of any other HttpServletRequest and the same. Analytical parameters in the request, servlet implementation of the necessary application logic, will respond to the sequence of XML, and write it back to HttpServletResponse.
</ Td> </ tr> <tr> <td height="20" colspan="2">
</ Td> </ tr> </ table>
Examples
I wrote the following is a simple example:
First, ajaxServlet.java procedures 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 * Create Date: 2005-10-25 * original author: miaohai * Copyright: miaohai * Function: Generating XML documents * * revised 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); response.getWriter PrintWriter out = (); String action = request.getParameter ( "action"); if (( "send"). equals (action)) (StringBuffer sb = new StringBuffer ( "
Sb.append ( "
Sb.append ( "
Sb.append ( "
Sb.append ( "
Out.write (sb.toString ()); out.close ();)))
<head>
</ Head>
<script Type="text/javascript">
/ * XMLHTTP through asynchronous transfer parameters to ajaxServlet sent back to the line with the conditions of XML documents * /
Function getResult () (var url = "/ Test / ajaxServlet? Send action =" 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);)) / * return of the 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] [i] = type. firstChild.data; document.all [ 'td']. innerHTML + = str [i] + "
";))))
</ Script>
<body OnLoad="getResult()">
<table Width="80%" border="0" cellspacing="0" cellpadding="0">
<tr> <td Id="td"> </ td> </ tr>
</ Table>
</ Body>
Third, web.xml file
<? Xml version = "1.0" encoding = "ISO-8859-1">
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Version = "2.4">
4. Diagram:
</ Td> </ tr> <tr>
↑ Back






