Express instance to use AJAX

2009-03-13  来源:本站原创  分类:AJAX  人气:611 

Summary Introduction:

AJAX-wide known as "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML), its core only JavaScript, XMLHTTPRequest and DOM.

XMLHTTP is the first application of Microsoft, IE (IE5 and above) by allowing developers at Web page for internal use XMLHTTP ActiveX components to expand their own capabilities, developers can not from the current Web page navigation and direct transmission of data to the server or from server check data. It can be excluded from the download redundant HTML required, thereby enhancing the speed of the process. Mozilla (Mozilla1.0 above and NetScape7 above) response is to create its own XML proxy class inheritance: XMLHttpRequest category. Konqueror (and Safari v1.2, is also the same KHTML-based browser) also supports the XMLHttpRequest object, and Opera will also be in its future versions v7.6x + support XMLHttpRequest object. For most cases, XMLHttpRequest and XMLHTTP object is very similar components, methods, and similar property, but has a small portion of property is not supported.

Examples:
The three source files <index.jsp, ValidateServlet.java, web.xml>

<index.jsp> reads as follows:

<% @ Page contentType = "text / html; charset = gb2312"%>
<! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN">
<html>
<head>

<title> ajax starting </ title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="description" content="desc">
<link rel="stylesheet" type="text/css" href="styles.css">

<script type="text/javascript">
var req;

function validate ()
(
var idField = document.getElementById ( "userid");
var url = "servlet / ValidateServlet? id =" + escape (idField.value);
if (window.XMLHttpRequest)
(
req = new XMLHttpRequest ();
)
else if (window.ActiveXObject)
(
req = new ActiveXObject ( "Microsoft.XMLHTTP");
)
if (req)
(
req.open ( "GET", url, true);
req.onreadystatechange = callback;
req.send (null);
)
)

function callback ()
(
if (req.readyState == 4)
(
if (req.status == 200)
(
parseMessage ();
)
else
(
setMessage ( "Not able to retrieve description" + req.statusText);
)
)
)

function parseMessage ()
(
var message = req.responseXML.getElementsByTagName ( "message") [0];
var name = req.responseXML.getElementsByTagName ( "name") [0];
setMessage (message.firstChild.data, name.firstChild.data);
)
function setMessage (message, name)
(
var userMessageElement = document.getElementById ( "userIdMessage");
userMessageElement.innerHTML = "<font color=\"red\">" + message + "you" + name + "</ font>";
)
</ script>
</ head>

<body>
<input type="text" size="20" name="id">

<div> </ div>
</ body>
</ html>

<ValidateServlet.java> Reads as follows:

package example;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ValidateServlet extends HttpServlet (

private ServletContext context;
private HashMap users = new HashMap ();

public ValidateServlet () (
super ();
)

public void destroy () (
super.destroy ();
)

public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException (
response.setContentType ( "text / xml");
response.setHeader ( "Cache-Control", "no-cache");

String targetId = request.getParameter ( "id");
System.out.println (targetId.trim ());

if ((targetId! = null) & & users.containsKey (targetId.trim ())) (

response.getWriter (). write ( "<info> <message> welcome </ message> <name>" + targetId.trim ()+"</ name> </ info> ");
) Else (

response.getWriter (). write ( "<info> <message> damp </ message> <name> bush </ name> </ info>");
System.out.print ( "invalid");
)
)

public void init (ServletConfig config) throws ServletException (
this.context = config.getServletContext ();
users.put ( "ben", "female");
users.put ( "ewilliams", "male");
)

)

<web.xml> reads as follows:

<? xml version = "1.0" encoding = "UTF-8"?>
<web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<welcome-file-list>
<welcome-file> index.jsp </ welcome-file>
</ welcome-file-list>

<servlet>
<servlet-name> ValidateServlet </ servlet-name>
<servlet-class> example.ValidateServlet </ servlet-class>
</ servlet>

<servlet-mapping>
<servlet-name> ValidateServlet </ servlet-name>
<url-pattern> / servlet / ValidateServlet </ url-pattern>
</ servlet-mapping>

</ web-app>

相关文章
  • Express instance to use AJAX 2009-03-13

    Summary Introduction: AJAX-wide known as "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML), its core only JavaScript, XMLHTTPRequest and DOM. XMLHTTP is the first application of Microsoft, IE (IE5 and above) by allowing develop

  • Instance using the Ajax library 8.2ASP.NET MVC2 2011-04-29

    ASP.NET MVC2 instance using the Ajax library . Net for JavaScript Use Jquery Update <h1> Product Search-jQuery </ h1> <Form action ="<%= Url.Action ("ProductSearch")%> "method =" post "" jform "

  • AJAX Advanced Learning (5) - AJAX ResponseXML instance 2010-12-27

    HTTP responseText return a string with different responses, responseXML the XML response is returned. ResponseXML property returns the XML document object, use the W3C DOM node tree methods and properties to check and parse the object. --------------

  • 128 ajax / javascript framework 2010-07-30

    128 ajax / javascript framework AJAX framework can help us to rapidly develop through JavaScript calling WebService (Server Page) pages, but not required to submit the entire page. Recent web applications use AJAX to improve the pages can be interact

  • 128 ajax / javascript frameworks 2010-07-30

    128 ajax / javascript framework AJAX framework can help us to rapidly develop through JavaScript calling WebService (Server Page) pages, but not required to submit the entire page. Recent web applications use AJAX to improve the pages can be interact

  • Ajax easy to understand tutorial examples [Original] 2010-12-12

    jsp page as follows: <% @ Page contentType = "text / html; charset = gb2312"%> <html> <head> <meta http-equiv=Content-Type content="text/html; charset=gb2312" /> <title> </ title> </ Head> <

  • AJAX Advanced Learning (4) - AJAX XML Example 2010-12-27

    AJAX can be used to interactive communication with the XML file. -------------------------- AJAX XML Example AJAX example below, we will demonstrate how to use AJAX technology, to read information from the XML file. -------------------------- Schemat

  • On SQL EXPRESS 2005 connection problems 2010-12-01

    The other one On SQL EXPRESS 2005 connection problems April 2, 2007 Monday 13:45 By default, SQL Server Express as a named "SQLEXPRESS" instance installation. Specified in the connection string name with the server instance name, you can connect

  • 學習 React.js:用 Node 和 React.js 創建一個實時的 Twitter 流 2015-04-17

    Build A Real-Time Twitter Stream with Node and React.js By Ken Wheeler (@ken_wheeler) 簡介 歡迎來到學習 React 的第二章,該系列文章將集中在怎麼熟練並且有效的使用臉書的 React 庫上.如果你沒有看過第一章,概念和起步,我非常建議你繼續看下去之前,回去看看. 今天我們準備創建用 React 來創建一個應用,通過 Isomorphic Javascript. Iso-啥? Isomorphic. Java

  • Comprehensive analysis of the XMLHttpRequest object 2010-04-02

    AJAX XMLHttpRequest object is present for all Web 2.0 applications and technology base. Although the software vendor and open source community are now providing AJAX framework in order to further simplify the use of XMLHttpRequest object; but we are

  • Comprehensive analysis of the XMLHttpRequest object (change) 2010-04-13

    AJAX XMLHttpRequest object is present for all Web 2.0 applications and technology base. Although the software vendor and open source community are now providing AJAX framework in order to further simplify the use of XMLHttpRequest object; but we are

  • prototype.js1.4 Edition Developer Guide 2010-08-21

    Some useful function in this library with many predefined objects and utility functions, such stuff was clearly aimed at you from some repetitive typing freed. Using $ () method $ () Method is used too frequently DOM document.getElementById () method

  • Geodatabase and ArcSDE 2010-09-08

    ArcGIS 9.2 product launch later, Geodatabase part of the larger changes occurred. This workshop presented are the Geodatabase and ArcSDE 9.2 features. The product composition: 9.1 version, Geodatabase consists of two: Personal Geodatabase and ArcSDE

  • SQL Server 2005 connection string 2010-09-15

    SQL Native Client ODBC Driver Standard secure connection Driver = (SQL Native Client); Server = myServerAddress; Database = myDataBase; Uid = myUsername; Pwd = myPassword; Are you using SQL Server 2005 Express? Please "Server" option to use to c

  • Very attractive web tier framework Itsnat (a) 2010-11-06

    From 2002 to use struts, to later JSF (04 started to pay attention, but the sun was the realization of a very unstable, many insects, so kick the tires a), and now with spring MVC, has recently started to pay attention to the wicket, java community w

  • Add new Item -> SQL Server 2008 Database.mdf 2011-04-19

    . Net novice, only for academic records only. To the project in VS2008 to add the desired service-based database file, select Project -> AddItem-> Data-> Service-Based Database will then report an error "Connections to SQL Server files (. Md

  • (Jsp language) Log instance (JS aggregation Enter key, and judging by Ajax user name, password, the correctness) 2010-08-23

    Here is a simple log instance (JS gather the enter key, and judging by Ajax user name, password correct) //jsp Page function checkManageLogin(){ var XMLHttp; XMLHttp = createXMLHttpRequest(); ajaxUrl = "<%=path%>/userAction.do?act=login&nam

  • JSP + Servlet + Ajax user information to achieve the background instance of the asynchronous test 2010-08-12

    Here are the contents of JSP front form submitted to the background by Ajax Servlet asynchronous calibrate (check a variety of ways, including the extraction of database information, check user name is repeated, etc.), asynchronous form in the JSP pa

  • Using the original access and use of jquery Ajax's visit to an instance of Ajax 2010-08-22

    Ajax's visit to the original <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xht ml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <

  • jQuery Ajax instance full resolution (Reprinted) 2010-10-28

    jQuery Ajax full-resolution examples <script type="text/javascript"> </ script> jQuery is really a very good lightweight JS framework, can help us quickly develop JS applications, and to some extent, changed the way we used to write