已经简单实现公聊、私聊功能
实现思路
阅读下面代码建议阅读顺序:login.jsp、 deal_login.jsp、main.jsp、list.jsp
看懂了这4个里面的application,LIst,session,框架,基本上就可以看得懂私聊公聊了,
这个jsp实验:
1、通过内存对象session 和 application 来保存信息
2、通过把application强制转化为list(链表)实现遍历所有的用户名或者信息
3、利用refresh函数来不断刷新,让聊天室实时更新用户以及聊天记录
这里用不同的浏览器打开,不能用同一个浏览器,否则没有私聊的效果
在编译器里面运行后复制链接到其他浏览器运行即可
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>title>
head>
<body>
<%
String msg = (String) request.getAttribute("msg");
if (msg == null) {
msg = "";
}
%>
<h1><%=msg%>h1>
<form action="Deal_Login.jsp" method="post" name="loginForm">
<p align="center">用户名:<input type="text" name="username" /> <br />
<p align="center"><input type="submit" value="登录" />
form>
body>
html>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.*"%>
<html>
<body>
<%
request.setCharacterEncoding("UTF-8");
//获取表单中提交过来的值
String username = request.getParameter("username");
//判断用户名是否为空
if (username.trim().length() == 0) {
//设置提示信息
request.setAttribute("msg", "请输入用户名");
request.getRequestDispatcher("login.jsp").forward(request,response);
return;
}
//从applicaton作用域中取出用户列表
List
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>title>
head>
<frameset rows="80%,*">
<frameset cols="20%,*">
<frame src="list.jsp"/>
<frame src="msg.jsp"/>
frameset>
<% response.setHeader("refresh", "10"); %>
<frame src="send.jsp"/>
frameset>
html>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.*"%>
<html>
<body>
<%
//从applicaton作用域中取出用户列表
List users = (List)application.getAttribute("users");
String username = (String) session.getAttribute("user");
//从该用户列表中移除该用户
users.remove(username);
application.setAttribute("users", users);
//注销该用户的会话
session.invalidate();
%>
<jsp:forward page="login.jsp"/>
body>
html>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.*"%>
<html>
<body>
<%
response.setHeader("refresh", "1");
//从applicaton作用域中取出用户列表
List users = (List) application.getAttribute("users");
out.print("目前在线的人有:"+users.size()+"人");
for (int i = 0; i < users.size(); i++) {
String username = (String) users.get(i);
out.print("" + username);
}
%>
body>
html>
1、当发给所有人的时候:每个人都显示
2、当from_sb发给to_sb的时候,只在这两个人的聊天框里面显示
3、from_sb 和 to_sb 两个application 保存的是每一条消息的发送方和接收方
4、最后暴力跑一遍即可
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.*"%>
<html>
<body>
<%
response.setHeader("refresh", "1");
List msgs = (List) application.getAttribute("msgs");
List to_sb = (List) application.getAttribute("private");
List from_sb = (List) application.getAttribute("S_private");
String user=(String)session.getAttribute("user");
try{
for (int i = 0; i < msgs.size(); i++) {
String t1 = (String) msgs.get(i);
String t2 = (String) to_sb.get(i);
String t3 = (String) from_sb.get(i);
if(t2.equals(user) || t2.equals("所有人") || t3.equals(user))
out.print(t1);
}
}catch(Exception e){}
%>
body>
html>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.*"%>
<html>
<body>
<form action="" method="post">
<p align="right">
<%=session.getAttribute("user")%>:发送消息给:
<select name="select">
<option value="0">所有人option>
<%
List users = (List) application.getAttribute("users");
for (int i = 0; i < users.size(); i++) {
String username = (String) users.get(i); %>
<option value="<%=i+1 %>"><%=username %>option>
<% } %>
select>
<input type="text" name="message" size="50" /><br />
<input type="submit" value="发送" />
<input type="button" value="退出登录" onClick="parent.location.href='loginOff.jsp'"/>
form>
<%
String user=(String)session.getAttribute("user");
request.setCharacterEncoding("UTF-8");
String message = request.getParameter("message");
String to="";
try{
//被发送消息方
List tmp = (List) application.getAttribute("users");
String SS = request.getParameter("select");
int a=Integer.parseInt(SS);
if(a==0) to="所有人";
else to=(String)tmp.get(a-1);
}
catch(Exception e){}
List msgs = (List) application.getAttribute("msgs");
if (msgs == null) {
msgs = new ArrayList();
}
List to_sb = (List) application.getAttribute("private");
if (to_sb == null) {
to_sb = new ArrayList();
}
List from_sb = (List) application.getAttribute("S_private");
if (from_sb == null) {
from_sb = new ArrayList();
}
if (message != null && !message.equals("")) {
msgs.add(user + "发消息给" + to + " : " +message + "
");
to_sb.add(to);
from_sb.add(user);
application.setAttribute("msgs", msgs);
application.setAttribute("private", to_sb);
application.setAttribute("S_private", from_sb);
}
%>
body>
html>