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("ÄúËùÇëÇóµÄÒ³ÃæÓÐÒì³£¡£");
Ïà¹ØÎĵµ£º
ÔÚJspÖе÷ÓÃSerlvetÖÐÒѾ´æ´¢ÁËÐÅÏ¢µÄ JavaBean.
ServletÖÐÒѾ°ÑÊý¾Ý¿âµÄÐÅÏ¢±£´æµ½JavaBeanÖÐ,ÔÙ°ÑJavaBean±£´æµ½SessionÖÐ.
Jsp¿ªÊ¼µ÷ÓÃ:
<jsp:useBean id="key" type="ClassName" scope="Session" />
<jsp:getProperty name="key" property="PropertyName" />
ÎÞ·¨¶Á³öÐÅÏ¢:
ÔÒò: JavaBean ÖÐÊôÐÔµ ......
<html>
<head>
<title>marquee</title>
<style>
#marq {
width:500px;
height:100px;
border:1px solid red;
overflow:hidden;
}
#mar ......
ǰ̨HTML´úÂë:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ÎÞ±êÌâÒ³</title>
<style ......
ÓйØajaxµÄÆäʵ¾ÍÊÇ1¸öÀàµÄʹÓÃ:XMLHttpRequest.
Ê×ÏÈÒªÉùÃ÷Õâ¸öÀàµÄ¶ÔÏ󣬼øÓÚ¸÷¸öä¯ÀÀÆ÷°ÑÕâ¸öÀàǶÈëµÄ·½Ê½²»Í¬£¬ÉùÃ÷µÄ·½Ê½Ò²²»Í¬£º
var xmlHttpReq = null;
if (window.XMLHttpRequest) //Mozilla ä¯ÀÀÆ÷
{
¡¡¡¡¡¡ xmlHttpReq = new XMLHttpRequest();
¡¡¡¡}
¡¡ else if (window.ActiveXObject) // ......
<%@ page info="Random Image Show"
pageEncoding="UTF-8" contentType="image/jpg"
autoFlush="true" buffer="16kb" session="false" import="java.io.FileInputStream"
%>< ......