日期:2014-05-19 浏览次数:20693 次
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</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="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="json2.js"></script> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> var xmlhttp; var length = 0; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } function query(){ json(); var t = setTimeout("query()", 1000); //每隔一秒向服务器发送请求 } function json(){ xmlhttp.onreadystatechange=function(){ alert(xmlhttp.readyState + " " + xmlhttp.status); //只有第一次能正常响应 if (xmlhttp.readyState==4 && xmlhttp.status==200) { var query = ""; var i = 0; var result = eval(xmlhttp.responseText); if(length < result.length){ for(;i < result.length; i++){ query = query + result[i].name + ":" + result[i].text + "\n"; } document.getElementById("query").value = query; length = result.length; } }} xmlhttp.open("GET","/Chat/json?time="+new Date(),true); xmlhttp.send(); } function add(){ var name = document.getElementById("name").value; var text = document.getElementById("text").value; xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { } } xmlhttp.open("GET","/Chat/json?name="+name+"&text="+text+"&mode=add&time="+new Date(),true); xmlhttp.send(); } </script> </head> <body onload="query()"> <textarea rows="5" cols="30" id="query"></textarea> <input type="text" id="name"> <textarea rows="5" cols="30" id="text" ></textarea> <input type="button" value="提交" onclick="add()"> </body> </html>