日期:2014-05-16  浏览次数:20702 次

AJAX注册1
一个MyEclipse里的AJAX例子2008-03-19 19:53本例将在页面的参数以Get和POST两种方式传递到服务器,并回显到页面;

本例共包括两个主要文件getAndPostExample.html和GetAndPostExample.java以及一个配置文件web.xml
建立文件的步骤:
1.在Eclipse新建一个web project-->ajax1
2.在ajax1里面新建一个getAndPostExample.html
3.在ajax1里面新建一个servlet-->GetAndPostExample.java

getAndPostExample.html如下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>getAndPostExamplel.html</title>
   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script type="text/javascript">
        var xmlHttp;
        //创建XMLhttpRequest对象
        function createXMLHttpRequest() {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
        }

        //生成传递到服务器的参数查询串,参数值由页面表单填写得到
       function createQueryString() {
            var firstName = document.getElementById("firstName").value;
            var middleName = document.getElementById("middleName").value;
            var birthday = document.getElementById("birthday").value;
            var queryString = "firstName=" + firstName + "&middleName=" + middleName
            + "&birthday=" + birthday;
            return queryString;
        }

        //点击按钮响应的Get方法主函数
        //Get方法把参数值以名=值方式在xmlHttp.open("GET", queryString, true)中传递,queryString的形式为URL?参数名=值&参数名=值...;而xmlHttp.send(null);
        function doRequestUsingGET() {
            createXMLHttpRequest();//第一步:创建XMLHttpRequest对象
            var queryString = "GetAndPostExample?";
            queryString = queryString + createQueryString()
            + "&timeStamp=" + new Date().getTime();//第二步:定义传递的参数值字符串
            xmlHttp.open("GET", queryString, true);//第三步:建立与服务器的请求
            xmlHttp.onreadystatechange = handleStateChange;//第四步:监听状态-->转到监听状态函数   
  &nb