ajax 是局部刷新,写了一段测试代码,用 application 代替了数据库 一个servlet input.jsp :用作输入 showInput.html :用作显示先打开 showInput.html ,然后在另外一个窗口打开 input.jsp 输入内容 输入后,showInput.html 同步更新import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AjaxServlet3 extends HttpServlet { public AjaxServlet3() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gbk"); PrintWriter out = response.getWriter(); out.println(request.getSession().getServletContext().getAttribute("inputStr")); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { }}showInput.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></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=GBK"> <script type="text/javascript"> var xmlHttp = null;
function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } }
function change(){ createXMLHttpRequest(); var url = "AjaxServlet3?" + Math.random(); xmlHttp.onreadystatechange = callback; xmlHttp.open("GET", url, true); xmlHttp.send(null); }
function callback(){ if (xmlHttp.readyState == 4){ if (xmlHttp.status == 200){ var str=xmlHttp.responseText; div1.innerHTML = str; } } }
b.jsp实现插入操作
当我从b实现插入后a一定要刷新才能够显示更新么
楼上2位的好象都要刷新啊 我就是不想刷新
就是说a b两个页同时打开着 b插入数据后a就能显示
一个servlet
input.jsp :用作输入
showInput.html :用作显示先打开 showInput.html ,然后在另外一个窗口打开 input.jsp 输入内容
输入后,showInput.html 同步更新import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxServlet3 extends HttpServlet { public AjaxServlet3() {
super();
} public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
out.println(request.getSession().getServletContext().getAttribute("inputStr"));
out.flush();
out.close();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} public void init() throws ServletException {
}}showInput.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></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=GBK">
<script type="text/javascript">
var xmlHttp = null;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function change(){
createXMLHttpRequest();
var url = "AjaxServlet3?" + Math.random();
xmlHttp.onreadystatechange = callback;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function callback(){
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
var str=xmlHttp.responseText;
div1.innerHTML = str;
}
}
}
function init(){
change();
setTimeout("init()",1000);
}
</script>
</head> <body onload="init()">
<div id="div1"></div>
</body>
</html>input.jsp<%@ page language="java" pageEncoding="GBK"%>
<%
String inputStr = request.getParameter("inputStr");
if(inputStr != null){
application.setAttribute("inputStr",inputStr);
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'input.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">
</head> <body>
<form action="input.jsp" method="post">
<input type="text" name="inputStr"/>
<input type="submit" value="模拟更新数据库"/>
</form>
</body>
</html>
哈哈,写错了
1.页面操作数据库,这个自然会刷新.
2.直接操作数据库,让页面刷新..这个ajax可以做但是貌似没有一点点用途,完全是耗费资源.如果你这个问题只是为了学习,那么这个不用学,没有人会这样用的.我是说正常人.
不好意思我好象没有表达清楚
用户a向服务器数据库提交了信息
要求该信息在已打开的用户b的查询页上动态的添加显示上去
感觉向是数据库在操作web样的
很郁闷
然后调用刷新
我没接触过ajax 希望高手可以给出代码让小弟参考 谢谢各位高手拉
你搜索下就很清楚了,你做这个跟做webim一摸一样的
我的开发环境是myeclipse+tomcat6.0
怎么弄这个东东咯
实战 Comet 应用程序开发http://www.ibm.com/developerworks/cn/web/wa-lo-w2fpak-comet/
一个WEB的IM,基于Pushlet实现http://www.javaeye.com/topic/89158
test.jsp<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>AJAX </title>
<script type="text/javascript">
var xmlHttpReq;
//创建XMLHTTP对象
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}else if(window.XMLHttpRequest){
xmlHttpReq = new XMLHttpRequest();
}
}
function change(){
createXMLHttpRequest();
var url = "test2.jsp?rand=" + Math.random();
xmlHttpReq.open("GET", url, true);
xmlHttpReq.onreadystatechange = callback;
xmlHttpReq.send(null);
}
function callback(){
if (xmlHttpReq.readyState == 4){
if (xmlHttpReq.status == 200){
document.getElementById("ajax").innerHTML = xmlHttpReq.responseText.replace(/\s*<\?/g, "<?");
}
}
}
function init(){
change();
setTimeout("init()",2000);
}
</script>
</head>
<body onload="init()"><div id="ajax"></div>
</body>
</html>
test2.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK" %><%
//连数据库
//取数据
//打印%>
另外这样的功能是有意思的,但是不是谁做就能做的好的。
听各位说了这么多 好象不管怎么样服务器和brower之间一点要保持连接
要么是服务器和brower保持连接 要么是brower定时向服务器请求
这样的话 时间长了后当访问客户多的时候
网络和服务器会挂掉啊
非常感谢各位的帮助