自制超简单JQuery AJAX聊天室

:D 上午无聊,做着玩玩。
不要鄙视我哟。哈哈


源码如下:
一共三个jsp

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>聊天室</title>
<script type="text/javascript" src="jquery-1.2.5.pack.js"></script>
</head>
<body>
<div id="content" style="font-size: 12px;overflow-y:auto ;height: 300px;width: 300px;border: 1px solid #CCC;padding: 10px;float: left;"></div>
<div id="member" style="overflow-y:auto ;height: 300px;width: 100px;border: 1px solid #CCC;padding: 10px;"></div>


<textarea rows="5" cols="35" id="say"></textarea><br/><input onclick="send();" type="button" value="发送"/>
<script type="text/javascript">

function inChat(){
$.ajax({
type: "POST",
url: "member.jsp",
success: function(msg){
getMember();
}
});
}

function send(){
$.ajax({
type: "POST",
url: "content.jsp",
data: "say="+$("#say").val(),
success: function(msg){
getContent();
$("#say").val("");
}
});

}
getContent();
getMember();
window.setInterval(function(){getContent();getMember();},3000);


function getContent(){
$.ajax({
type: "POST",
url: "content.jsp",
success: function(msg){
$("#content").html(msg);
}
});
}

function getMember(){
$.ajax({
type: "POST",
url: "member.jsp",
success: function(msg){
$("#member").html(msg);
}
});
}
</script>
</body>
</html>



member.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
if(application.getAttribute("member")==null)
application.setAttribute("member",new java.util.HashSet());

java.util.Set member=(java.util.Set)application.getAttribute("member");
member.add(request.getRemoteAddr());
%>

<% for(Object o:member){%>
<%=o%><br/>
<%}%>



content.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
if(application.getAttribute("content")==null)application.setAttribute("content","");
%>
<%
if(request.getParameter("say")!=null)
application.setAttribute("content",application.getAttribute("content")+"<br/>"+request.getRemoteAddr()+": "+request.getParameter("say"));
%>
<%=application.getAttribute("content")%>



把这三个页面和jquery-1.2.5.pack.js包放在WebContent下就可以了,有乱码问题的话加个编码过滤器就可以了。

我在web.xml里面加了spring现成的过滤器 :D

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值