无刷新显示数据(摘抄)修改。ajax。献给和我一样的人。防止大家看到一堆堆的代码,拿来不能用,改还不知道从那里入手,给人家发消息还不回的人

来源:互联网 发布:全国计算机二级c语言 编辑:程序博客网 时间:2024/05/16 15:12

AJAXEXAMPLE.html   显示也是这个文件

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<body>
<script type="text/javascript">
function showCustomer(str)
{
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
     var xmlHttp=null;
       if(window.ActiveXObject)
    { 
   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="autolist.jsp";
//url=url+"?q="+str;
//url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function()
{
  if (xmlHttp.readyState==4)
{
  
               document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
                setTimeout("showCustomer('001')", 1000);       //这个我是为了查询数据 编号是001的。

 
}
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
</script>
<form>
Select a Customer:
<select name="customers" onchange="showCustomer(this.value)">
<option value="001">Alfreds Futterkiste
<option value="002 ">North/South
<option value="003">Wolski Zajazd
</select>
<marquee><div id="txtHint"><b>Customer info will be listed here.</b></div><br></marquee>
</form><p>

</p>
</html>
<script language="JavaScript">

  
 window.setTimeout("showCustomer('001')", 1000);
 
  
</script> 

 

autolist.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"
    import="java.sql.*"
   import="java.io.*"
   import="java.util.*"
   import="javax.sql.*"
   import="javax.naming.*"
   import="java.text.*"
   import="java.lang.*"
%>

<%
        //设置输出信息的格式及字符集   
     CB_action="ddddddd";    
        response.setContentType("text/xml; charset=UTF-8");
        response.setHeader("Cache-Control","no-cache");
        out.println("<response>");        
              for(int i=0;i<2;i++){
         out.println("<time>"+CB_action+"</time>");
           System.out.println("检查监测项目CB_action*****:="+CB_action);
    }
     try {
         out.println("</response>");
        // out.close();
       }
       catch (Exception ex) {
         ex.printStackTrace();
       }

%>

 

下面是别人的也能用,
autorefresh.jsp      //调用的也是它

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script language="javascript">
    var XMLHttpReq;
     //创建XMLHttpRequest对象      
    function createXMLHttpRequest() {
        if(window.XMLHttpRequest) { //Mozilla 浏览器
            XMLHttpReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { // IE浏览器
            try {
                XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    }
    //发送请求函数
    function sendRequest() {
        createXMLHttpRequest();
        var url = "autolist.jsp";
        XMLHttpReq.open("GET", url, true);
        XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
        XMLHttpReq.send(null);  // 发送请求
    }
    // 处理返回信息函数
    function processResponse() {
        if (XMLHttpReq.readyState == 4) { // 判断对象状态
            if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
                DisplayHot();
             //   setTimeout("sendRequest()", 10000);
            } else { //页面不正常
                window.alert("您所请求的页面有异常。");
            }
        }
    }
    function DisplayHot() {
        var name = XMLHttpReq.responseXML.getElementsByTagName("time")[0].firstChild.nodeValue;
        document.getElementById("time").innerHTML = name;   
    }

</SCRIPT>


<body  onload =sendRequest()>
<table  style="BORDER-COLLAPSE: collapse" borderColor=#111111
            cellSpacing=0 cellPadding=0 width=100% bgColor=#f5efe7 border=0>
    <tr>
        <td height="20" id="time">
        </td>
      </tr>
  
</table>

</body>
</html>
<script language="JavaScript">

  
 window.setTimeout("sendRequest()", 1000);
 
  
</script>

 

 

autolist.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"
    import="java.sql.*"
   import="java.io.*"
   import="java.util.*"
   import="javax.sql.*"
   import="javax.naming.*"
   import="java.text.*"
   import="java.lang.*"
%>

<%
        //设置输出信息的格式及字符集   
     CB_action="ddddddd";    
        response.setContentType("text/xml; charset=UTF-8");
        response.setHeader("Cache-Control","no-cache");
        out.println("<response>");        
              for(int i=0;i<2;i++){
         out.println("<time>"+CB_action+"</time>");
           System.out.println("检查监测项目CB_action*****:="+CB_action);
    }
     try {
         out.println("</response>");
        // out.close();
       }
       catch (Exception ex) {
         ex.printStackTrace();
       }

%>


原创粉丝点击