我用一个jsp页显示从mssql中查询出来的结果
当数据库中的数据有添加或修改的时候  我希望jsp页能够动态的显示修改
而不用去刷新jsp页

解决方案 »

  1.   

    用setTimeout定时调用ajax方法,来刷新页面
      

  2.   

    比如我用a.jsp来显示数据库的查询结果 
    b.jsp实现插入操作
    当我从b实现插入后a一定要刷新才能够显示更新么
    楼上2位的好象都要刷新啊  我就是不想刷新
    就是说a b两个页同时打开着  b插入数据后a就能显示
      

  3.   

    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;
    }
    }
    }

    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>
      

  4.   

    comet
    哈哈,写错了
      

  5.   

    如果你是在项目中的需要,那么有两种情况:
    1.页面操作数据库,这个自然会刷新.
    2.直接操作数据库,让页面刷新..这个ajax可以做但是貌似没有一点点用途,完全是耗费资源.如果你这个问题只是为了学习,那么这个不用学,没有人会这样用的.我是说正常人.
      

  6.   

    谢谢了
    不好意思我好象没有表达清楚
    用户a向服务器数据库提交了信息 
    要求该信息在已打开的用户b的查询页上动态的添加显示上去
    感觉向是数据库在操作web样的  
    很郁闷
      

  7.   

    我想应该是要用ajax定时的探测数据库看有没有更新
    然后调用刷新
    我没接触过ajax   希望高手可以给出代码让小弟参考 谢谢各位高手拉
      

  8.   

    人家说了,这是很浪费资源的,如果只是一般的使用没必要这么做,如果一定要就用setTimeout使Ajax定时去调用function局部刷新div
      

  9.   

    那就用AJAX吧,定时刷新数据库数据,具体代码手头上还没有,网上搜一下应该有的
      

  10.   

    comet 
    你搜索下就很清楚了,你做这个跟做webim一摸一样的
      

  11.   

    用ajax   服务器推  JAVA阵营     QQ群   68575630
      

  12.   

    高手救我啊
    我的开发环境是myeclipse+tomcat6.0
    怎么弄这个东东咯
      

  13.   

    comet基本介绍http://www.ibm.com/developerworks/cn/web/wa-lo-comet/
    实战 Comet 应用程序开发http://www.ibm.com/developerworks/cn/web/wa-lo-w2fpak-comet/
    一个WEB的IM,基于Pushlet实现http://www.javaeye.com/topic/89158
      

  14.   

    用setTimeout定时调用ajax方法,把时间设置的小点,同样是看不出来刷新的,这个是最简单的方式
      

  15.   

    给你最简单的那个实现吧跟5楼几乎一摸一样,这样实现肉眼是根本看不出来刷新的
    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" %><%
    //连数据库
    //取数据
             //打印%>
      

  16.   

    或者将轮循放服务器端,因为ajax得不到返回结果会等待。客户端资源就相对好一点了。服务器端java做轮循的效率肯定比js高。
    另外这样的功能是有意思的,但是不是谁做就能做的好的。
      

  17.   

    哎  难道这是bs的一个缺点么  
    听各位说了这么多   好象不管怎么样服务器和brower之间一点要保持连接
    要么是服务器和brower保持连接  要么是brower定时向服务器请求
    这样的话 时间长了后当访问客户多的时候  
    网络和服务器会挂掉啊
      

  18.   

    确实是bs的缺点,因为bs是机遇无状态的http协议的,还有一个情况,用socket,这样等待的时间不占资源。当然bs里没人会这么用。
      

  19.   

    搞定拉  还是用了ajax定时刷的 
    非常感谢各位的帮助