刚学web编程,老师让写一个网络聊天室,发现诸多问题无法解决,在此询问诸位大神,万望搭救兄弟一把。
1.当A发送消息时,在他自己的页面中可以刷出自己的聊天信息来,B的页面没有显示,只有当B也发送一条信息后,A的聊天信息才会连同自己的一起显示。这是怎么回事?怎么解决?
2.怎么使得滚动条每发送一条信息就自动滚到最下方?
3.我用的是textarea显示聊天记录,怎么能显示用户的头像呢?(用户头像存储在了数据库中,在用户表里)
4.好友列表是用JSP写的,怎么能实时显示在线用户,即有人登陆即出现在好友列表中,有人下线即从好友列表中消失?
万望大神搭救。以下是聊天页面的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import=" javax.servlet.http.HttpServletRequest"%>
<%@ page import=" javax.servlet.http.HttpServletResponse"%>
<%@ page import=" javax.servlet.http.HttpSession"%>
<%@ page import=" java.util.Date" %>
<!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>Insert title here</title>
<style>
#div2{
position:absolute;
top:10px;
bottom:140px;
left:10px;
right:10px;
width:95%;
background:#F0F0F0;
}
#div1{
position:absolute;
bottom:10px;
width:95%;
height:100px;
margin:0;
}
#text{
margin:0;
width:84%;
padding:0;
height:100px;
float:left;
}
#text2{
margin:0;
width:100%;
height:100%;
}
#button{
margin:0;
width:15%;
padding:0;
height:100px;
float:right;
}
</style>
<script type="text/javascript">
function check(){
if(document.getElementById("text").value.length==0){
document.getElementById("text").value="聊天记录不能为空";
return false;
}
else{
localtion.reload();
return true;
}
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String chat=request.getParameter("text");
String chats=(String)application.getAttribute("chat");
if(chat!=null){
chat=new String(chat.getBytes("ISO-8859-1"),"UTF-8");
String username;
HttpSession session1=request.getSession();
if(session1.getAttribute("username")!=null){
username=(String)session1.getAttribute("username");
}
else username="游客";
Date d=new Date();
chat=username+" "+d.toLocaleString()+"\r\n"+chat;
if(chats==null)
chats=chat;
else chats=chats+"\r\n"+"\r\n"+chat;
}
if(chats!=null)
application.setAttribute("chat", chats);%>
<div id="div2"><textarea id="text2" ><%out.println(application.getAttribute("chat"));%></textarea></div>
<form name="form1" id="form1" method="get" action="chatting.jsp" onsubmit="return check()">
<div id="div1">
<div><textarea rows="6" cols="10" id="text" name="text"></textarea></div>
<div><input type="submit" id="button" name="button" value="发言"></div>
</div>
</form>
</body>
</html>