jsp+ajax×Ô¶¯Ë¢ÐÂʵÀý(ת)
Ò»¡¢´¦ÀíajaxÇëÇóµÄjspÎļþ£ºauto.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%
//ÉèÖÃÊä³öÐÅÏ¢µÄ¸ñʽ¼°×Ö·û¼¯
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("<name>"+(int)(Math.random()*10)+
"ÐͱʼDZ¾</name>");
out.println("<count>" +(int)(Math.random()*100)+ "</count>");
}
out.println("</response>");
out.close();
%>
¶þ¡¢·¢ËÍajaxÇëÇóµÄhtmlÎļþ£ºautoRefresh.html
<head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
</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 = "auto.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()", 1000);
} else { //Ò³Ãæ²»Õý³£
window.alert("ÄúËùÇëÇóµÄÒ³ÃæÓÐÒì³£¡£");
Ïà¹ØÎĵµ£º
Case I. Web´úÀíµÄ·½Ê½ (on Server A)
¼´Óû§·ÃÎÊAÍøվʱËù²úÉúµÄ¶ÔBÍøÕ¾µÄ¿çÓò·ÃÎÊÇëÇó¾ùÌá½»µ½AÍøÕ¾µÄÖ¸¶¨Ò³Ã棬ÓɸÃÒ³Ãæ´úÌæÓû§Ò³ÃæÍê³É½»»¥£¬´Ó¶ø·µ»ØºÏÊʵĽá¹û¡£´Ë·½°¸¿ÉÒÔ½â¾öÏֽ׶ÎËùÄܹ»Ïëµ½µÄ¶àÊý¿çÓò·ÃÎÊÎÊÌ⣬µ«ÒªÇóAÍøÕ¾ÌṩWeb´úÀíµÄÖ§³Ö£¬Òò´ËAÍøÕ¾ÓëBÍøÕ¾Ö®¼ä±ØÐëÊǽôÃÜÐ×÷µÄ£¬ÇÒÿ´Î½»»¥¹ý³Ì£¬AÍøÕ¾µÄ· ......
1£º²éÕÒÒ³ÃæµÄ´úÂë´íÎóÓÌÈçÒ½Éú¸ø²¡ÈË¿´²¡£¬²»ÄÜͷʹҽͷ£¬½ÅÍ´Ò½½Å£¬¾ÍÊÇ˵һ¸öµØ·½µÄ“´íÎó”ÍùÍùÊÇÓɱðµÄµØ·½ÓÐÎÊÌâ¶øÒý·¢µÄ¡£
2£ºÈç¹ûÊý¾Ý·ÅÔÚrequestÖÐ(¿ÉÄÜ°üÀ¨url²ÎÊý)£¬ÄÇôҪÔÚºóÐøµÄÒ³Ãæ(»òÊÇˢкó)Öп´µ½ÒÔÇ°µÄÊý¾Ý¿ÉÒÔʹÓÃ${param.yourdate},²»¹ý·ÅÔÚActionFormÖпÉÄܸüºÃһЩ¡£
3£ºÔÚJSPÒ³ ......
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
--from MyEclipse, automatically generate. ......