jsp聊天程序

w_j123 2009-03-23 03:32:52
我是刚学jsp的 老师让用jsp做一个简单的聊天程序 请高手给点指点 谢谢喽
...全文
95 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
w_j123 2009-03-23
  • 打赏
  • 举报
回复
非常感谢啊 应该可以用自动刷新吧 像refresh应该可以自动刷新的 O(∩_∩)O哈哈~
hoojo 2009-03-23
  • 打赏
  • 举报
回复

main.jsp
<frameset rows="85%,*">


<frameset cols="80%,*">


<frame src="message.jsp" name="message" />

<frame src="userList.jsp" name="userList">


</frameset>

<frame src="sendMessage.jsp" name="sendMessage" />

</frameset>
<noframes></noframes>



message.jsp
<%
request.setCharacterEncoding("gbk");
List messageList = (List) application.getAttribute("messageList");
String message = request.getParameter("message");
Users user = (Users) session.getAttribute("user");

if (messageList == null || messageList.size() == 0) {
messageList = new ArrayList();
application.setAttribute("messageList",messageList);
}
if (message != null && !"".equals(message)) {
messageList.add("<img src='../image/iboy.gif' /> <font color='blue'>"+user.getUserName() + " <b>说:</b></font><br />         "
+ message);
}
%>
<table width="100%" border="0" align="left" cellpadding="0"
cellspacing="0">

<%
for (int i = 0; i < messageList.size(); i++) {
String userName = (String) messageList.get(i);

%>


<tr align="left">
<td height="20" class="p16"><%=userName%></td>
</tr>

<%

}
%>
</table>

registerManage.jsp
<%
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String param = request.getParameter("param");//获取param参数,用以判断用户操作是注册还是登录

UserDao userDao = new UserDao();
//判断用户输入信息是否为空
if (param == null || userName == null || password == null) {
response.sendRedirect("register.jsp");
} else if (param != null && "login".equals(param)) { //登录操作
//根据用户名和密码查询数据库
Users user = userDao.findUsers(userName, password);
if (user != null) { //判断当前用户是否存在
//如果用户存在,把当前用户名保存到application中的List集合内
List userList = (List) application.getAttribute("users");
if (userList == null || userList.size() == 0) { //针对第一个登录用户的操作
userList = new ArrayList();
userList.add(userName);
} else { //针对其他用户的操作
boolean bool = true;
for (int i = 0; i < userList.size(); i++) {

if (userName.equals(userList.get(i).toString())) {
bool = false;
break;
}
}
if (bool)
userList.add(userName);
}
//把当前用户的信息保存到session中
session.setAttribute("user", user);
application.setAttribute("users", userList);
response.sendRedirect("main.jsp");
} else{ //如果用户不存在,给出提示后,返回到网站首页
//response.sendRedirect("register.jsp");
out
.print("<script type='text/javascript'>alert('用户名或密码错误,请重新登录。');location.replace('index.jsp');</script>");
}
} else { //注册操作
int count = userDao.insertUser(userName, password,0);
if (count > 0) {
out
.print("<script type='text/javascript'>alert('注册成功,请登录。');location.replace('index.jsp');</script>");
} else {
out
.print("<script type='text/javascript'>alert('注册失败,请重新注册。');history.go(-1);</script>");
}
}
%>

sendManage.jsp
<script type="text/javascript">
function checkSend(){
if(document.send.msg.value==""){
alert("不能发送空信息。");
return false;
}else{
document.send.message.value=document.send.msg.value;
document.send.msg.value="";
return true;
}

}
</script>
</head>

<body>
<form action="message.jsp" method="post" name="send" onSubmit="return checkSend()" target="message">
<input type="hidden" name="message" value="" />
<p align="center">
请输入发送信息:
<input type="text" name="msg" size="30" />      
<input type="submit" value="发送" />
</p>
</form>
</body>

userList.jsp
<%

List userList = (List) application.getAttribute("users");
// Users user=(Users) session.getAttribute("users");
%>

<html>
<head>

<title>message</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="refresh" content="3">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body onload="scroll(0,9999)">
<table width="100%" border="0" align="left" cellpadding="0"
cellspacing="0" bordercolor="#EBEBEB">
<%
for (int i = 0; i < userList.size(); i++) {
String userName = (String) userList.get(i);

%>


<tr align="left">
<td height="20"class="p16"><img src="../image/iboy.gif" /> <%=userName%></td>
</tr>
<%
}
%>
</table>


这样就可以了,所有的代码均没有错误!
我做好的,缺点是要不断的刷新页面!
每次只能看到一条消息。
可以换种思想,把消息写到数据库中。
不过要不断的查询数据库,可以定时每5分钟
把消息写入数据库,然后查询出来。
这样可能好点。
先看看,以上的方法吧!
^_@
oklinsong 2009-03-23
  • 打赏
  • 举报
回复
mark
w_j123 2009-03-23
  • 打赏
  • 举报
回复
最好是多人聊天室 (*^__^*) 嘻嘻……
zhuhong110450 2009-03-23
  • 打赏
  • 举报
回复
请问是单人聊天室还是多人聊天室??
w_j123 2009-03-23
  • 打赏
  • 举报
回复
先谢谢了 老师说用到iframe 但是因为从没做过所以没有思路啊 能不能再具体点啊
hoojo 2009-03-23
  • 打赏
  • 举报
回复
提示:
将用户发送的消息保存在List 中

Appliction.setAttribute("list",list);

最后取出来
显示在页面上

注意第一次的时候要 创建List
以后就直接list.add 就可以了
^_@

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧