谁有ajax留言板的例子?

解决方案 »

  1.   

    我曾经试图想在静态环境下仅用js&xml实现留言板功能
    但是好像不是很可能
    找不到js如何远程操作服务器上的xml文件的方法
    如果有高手能解决
    或者有例子就请分享一下啊
      

  2.   

    AUTHOR:imA(男的不会,会的不男) 
    写了一个简单的例子,就是前端的一个html页面不断向后台servlet请求,后台servlet每次生成
    一个随机的long,前台html每次都显示这个long值,你可以找个tomocat服务器部署一下。
    index.html:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Title</title><script>
    function Recieve()
    {if(window.XMLHttpRequest)
    {
    xmlHttp=new XMLHttpRequest()
    }
    else if(window.ActiveXObject)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    xmlHttp.onreadystatechange=function()
    {
    if((xmlHttp.readystate==4)&&(xmlHttp.status==200))
    {
    text=xmlHttp.responseText;
    document.getElementById("view").value=text;
    }
    }
    xmlHttp.open("POST","/webchat/ViewServlet",true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send();
    }
    window.setInterval("Recieve()",60);//设置定时执行,60是执行间隔,单位是毫秒
    </script></head>
    <body>
    <input id="view" type="text" readonly="readonly"/>
    </body>
    </html>Servlet.java:
    package com.servlet;import java.io.IOException;
    import java.util.Random;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse; public class ViewServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {public ViewServlet() {
    super();
    }   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Random r=new Random();
    long l=r.nextLong();
    response.getWriter().write(""+l);
    response.getWriter().flush();
    }         
    }
    --------------------
    servlet的映射:
    <servlet>
    <description>
    </description>
    <display-name>
    ViewServlet</display-name>
    <servlet-name>ViewServlet</servlet-name>
    <servlet-class>
    com.servlet.ViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>ViewServlet</servlet-name>
    <url-pattern>/ViewServlet</url-pattern>
    </servlet-mapping>其中在html中的xmlHttp.open("POST","/webchat/ViewServlet",true);
    中的webchat是我的应用项目的名称,你可以改成你自己的,然后把上面的servlet和html文件部署
    到你自己的应用上,然后在浏览器中输入访问这个html的地址,可以看到效果。
      

  3.   

    lurkerxh(lurkerxhxh) 给我发份到邮箱嘛,谢谢
    [email protected]