当前页面: 开发资料首页 → JSP 专题 → 谁有ajax留言板的例子?
谁有ajax留言板的例子?
摘要: 谁有ajax留言板的例子?
谁有ajax留言板的例子?
帮你UP
没有,网上应该能够搜到
我曾经试图想在静态环境下仅用js&xml实现留言板功能
但是好像不是很可能
找不到js如何远程操作服务器上的xml文件的方法
如果有高手能解决
或者有例子就请分享一下啊
AUTHOR:imA(男的不会,会的不男)
写了一个简单的例子,就是前端的一个html页面不断向后台servlet请求,后台servlet每次生成
一个随机的long,前台html每次都显示这个long值,你可以找个tomocat服务器部署一下。
index.html:
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
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>
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的映射:
ViewServlet
ViewServlet
com.servlet.ViewServlet
ViewServlet
/ViewServlet
其中在html中的xmlHttp.open("POST","/webchat/ViewServlet",true);
中的webchat是我的应用项目的名称,你可以改成你自己的,然后把上面的servlet和html文件部署
到你自己的应用上,然后在浏览器中输入访问这个html的地址,可以看到效果。
我有例子加我qq283471442
顶。。。。
lurkerxh(lurkerxhxh) 给我发份到邮箱嘛,谢谢
mailsunlin@gmail.com