Web聊天程序

这是一个利用JSP设计的一个简单的Web聊天程序,其功能类似QQ的群聊。

用户输入用户名姓名登录,进入聊天页面,该页面可以发送并显示聊天信息。若用户已经登录,则不可重复登录。用户登录成功,在聊天内容窗口显示该用户上线。程序中用application对象保存聊天内容和全部登录用户名,用session对象保存用户登录信息,用request和response对象进行页面参数传递。有以下六个文件。


一、login.jsp——用户登录页面

显示用户名输入框。单击登录按钮,若该用户已登录,则会显示提示,否则提交login.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>WEB聊天--登录页</title>
</head>
<body>
<% String loginmsg = (String) request.getAttribute("loginmsg"); 
if(loginmsg==null){loginmsg="";}%>
<center><h2>
<%=loginmsg %></h2></center><br>
<form action="loginpro.jsp" method="post" name="logform">
<center>用户名:<input type="text" name="usrname"></center><br><br>
<center><input type="submit" value="登录"></center></form>
</body>
</html>

二、loginpro.jsp——用户登录处理程序

通过application对象判断用户是否登录。若已登录,通过request对象传回login.jsp页面显示。若未登录,将该用户信息加入application和session对象,并跳转到page.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=ISO-8859-1">
<title>聊天--用户登录处理</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8");
String usrname=request.getParameter("usrname");
if(usrname.trim().length()==0){
request.setAttribute("loginmsg", "请输入用户名");
request.getRequestDispatcher("login.jsp").forward(request,response);
return;}
String users;
if(application.getAttribute("users")==null)
users="";
else
users=(String)application.getAttribute("users");
if(users!=""){
if(users.indexOf(usrname)==-1)
{
users=users+','+usrname;
application.setAttribute("users",users);}
else{
request.setAttribute("loginmsg","用户"+usrname+"已登录,请输入其他用户名");
request.getRequestDispatcher("login.jsp").forward(request,response);
return;}}
else
application.setAttribute("users",usrname);
session.setAttribute("user",usrname);
response.sendRedirect("page.jsp");
%>
</body>
</html>

三、page.jsp——主页面

该页面由上、下两个frame构成,上方frame显示聊天内容,下方是用户信息发送窗口,分别加载listmsg.jsp和sendmsg.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=ISO-8859-1">
<title>聊天--主页</title>
</head>
<frameset rows="70%,*">
<frame src="listmsg.jsp">
<frame src="sendmsg.jsp"></frameset>
</html>

四、listmsg.jsp——从application对象中读取用户发送的消息,每隔3秒刷新。

<%@ 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=ISO-8859-1">
<title>聊天--显示聊天内容</title>
</head>
<body>
<%
response.setHeader("refresh","3");
String msgs=(String) application.getAttribute("msgs");
if(application.getAttribute("msgs")==null)
out.print("聊天程序启动");
else
out.print(msgs);%>
</body>
</html>

五、sendmsg.jsp——单击发送按钮,将文本框中的内容经过处理后加载到application对象的msg属性中。单击退出按钮,转入logout.jsp。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="java.util.*,java.util.Date,java.text.SimpleDateFormat"%>
<!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=ISO-8859-1">
<title>聊天--消息发送窗口</title>
</head>
<body>
<form action="" method="post">
<%=session.getAttribute("user") %>,发言:
<input type="text" name="message" size="40"><br><br>
<input type="submit" value="发言">
<input type="button" value="退出" onClick="parent.location.href='logout.jsp"></form>
<%
String msgs=(String)application.getAttribute("msgs");
String user=(String)session.getAttribute("user");
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s=df.format(new Date());
request.setCharacterEncoding("UTF-8");
String message=request.getParameter("message");
if(application.getAttribute("msgs")!=null){
if(request.getParameter("message")==null)
msgs=msgs+"<br>"+user+":上线( "+s+")";
else
msgs=msgs+"<br>"+user+ "说: "+message+ " ("+s+")";
}
else{
if(request.getParameter("message")==null)
msgs=user+":上线 ( "+s+")";
else
msgs="<br>";}
application.setAttribute("msgs",msgs);%>
</body>
</html>

六、logout.jsp——从application对象的users属性中移除该用户并注销其会话。

<%@ 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=ISO-8859-1">
<title>聊天--用户退出</title>
</head>
<body>
<%
String users=(String)application.getAttribute("users");
String username=(String)session.getAttribute("user");
String s[]=username.split(",");
String newusers="";
if(!s[0].equals(username))
newusers=s[0];
for(int i=1;i<s.length;i++){
if(!s[i].equals(username)){
if(newusers!="")
newusers=newusers+","+s[i];
else
newusers=s[i];
}
}
application.setAttribute("users",newusers);
session.invalidate();%>
<jsp:forward page="login.jsp">
</body>
</html>


程序运行时,先显示登录页面。

若该用户已登录,则会给出提示信息,并要求重新输入用户名。

用户输入登录名后,进入主页面,用户发送消息后,会在消息窗口显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值