首页 > 代码库 > 聊天室
聊天室
此聊天室用Servlet和jsp实现,功能包括统计在线人数,查看在线用户,上传文件,显示用户聊天记录。还有待修改的地方,适合入门。
工程文件包括login.jsp,main.jsp,display.jsp,sponse.jsp,shangchuan.jsp,sc.jsp,userlist.jsp
统计在线人数用到servlet监听器userlistener.java
package listener; import java.util.ArrayList; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class userlistener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener{ ServletContext app=null; @Override public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub Integer num=(Integer)app.getAttribute("onlinecount"); app.setAttribute("onlinecount", num+1); String user=(String) se.getSession().getAttribute("username"); ArrayList<String> ar=(ArrayList<String>)app.getAttribute("name"); ar.add(user); app.setAttribute("name", ar); } @Override public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub Integer num=(Integer)app.getAttribute("onlinecount"); app.setAttribute("onlinecount", num-1); ArrayList<String> ar=(ArrayList<String>)app.getAttribute("name"); String user=(String) se.getSession().getAttribute("username"); ar.remove(user); app.setAttribute("name", ar); } @Override public void attributeAdded(HttpSessionBindingEvent se) { // TODO Auto-generated method stub ArrayList<String> array=(ArrayList<String>)app.getAttribute("name"); String user=(String)se.getValue(); array.add(user); } @Override public void attributeRemoved(HttpSessionBindingEvent se) { // TODO Auto-generated method stub ArrayList<String> array=(ArrayList<String>)app.getAttribute("name"); String user=(String)se.getValue(); array.remove(user); } @Override public void attributeReplaced(HttpSessionBindingEvent event) { // TODO Auto-generated method stub } @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub app.removeAttribute("name"); app.setAttribute("onlinecount", 0); } @Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub ArrayList<String> array=new ArrayList<String>(); app=sce.getServletContext(); app.setAttribute("name",array); app.setAttribute("onlinecount", 0); } }
登录界面
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="listener.userlistener" %> <html> <head> <title>My JSP 'login.jsp' starting page</title> </head> <body bgcolor="green"> <% request.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=gb2312"); %> <center> <h5>欢迎来到聊天室</h5> <h1>艳子茶访</h1> <form action="main.jsp" method="post"> <table> <tr> <td>用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="mima"></td> </tr> </table> <tr> <td><input type="submit" value=http://www.mamicode.com/"登录">>聊天室界面此页面由两个框架组成
main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=gb2312"); %> <html> <head> <title>聊天主页面</title> </head> <% application.setAttribute("message", ""); String na=request.getParameter("username"); session.setAttribute("username", na); %> <frameset rows="*,100" frameborder="no"> <frame src=http://www.mamicode.com/display.jsp name=display frameborder="no" scrolling="auto">>
上半部分的display.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="listener.userlistener" %> <% response.setHeader("refresh","5"); %> <html> <head> <title>My JSP 'display.jsp' starting page</title> </head> <body bgcolor=green> <% request.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=gb2312"); %> <form action="" method="post"> <p align="center"><font size=5>聊天记录</font></p> <p align="right"><font size=2>目前在线人数:<%=application.getAttribute("onlinecount") %></font></p> <hr> <% String str=application.getAttribute("message").toString(); if(!str.equals("")&&str!=null){ out.print(str); } %> </form> </body> </html>下半部分的sponse.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'sponse.jsp' starting page</title> <script language="javaScript"> function qinkong(){ application.removeAttribute("message"); } </script> </head> <body bgcolor=green> <% request.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=gb2312"); %> <form action="" method="post"> <p align="center"> 我要发言: <input type="text" name="message" size="70"> <input type="submit" value=http://www.mamicode.com/"发送">>上传文件页面实现上传功能需要用到jspSmartUpload.jar包
shangchuan.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>文件上传</title> </head> <body bgcolor=green> <p align="center">请选择你要上传的文件</p> <form action="sc.jsp" method="post" enctype="multipart/form-data"> <input type="hidden" name="TEST" value=http://www.mamicode.com/"good">>上传一个shang.txt文件到工程目录mulu成功后的处理页面
sc.jsp<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312" errorPage=""%> <%@ page import="com.jspsmart.upload.*" %> <jsp:useBean id="SU" scope="page" class="com.jspsmart.upload.SmartUpload"/> <html> <head> <title>上传文件</title> <meta http-equiv="Content-Type" content="text/html;charset=2312"> </head> <body bgcolor=green> <% try{ SU.initialize(pageContext); SU.service(request, response); SU.upload(); String fn=SU.getFiles().getFile(0).getFileName(); SU.save("mulu/"); out.println("<br>上传成功查看<a href=http://www.mamicode.com/mulu/"+fn+">mulu/"+fn+"文件");>查看用户列表
uselist.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'userlist.jsp' starting page</title> </head> <body bgcolor=green> <center> <h3>用户列表</h3> <% ArrayList<String> array=(ArrayList<String>)application.getAttribute("name"); Iterator<String> it=array.iterator(); while(it.hasNext()){ String str=it.next(); out.print("**"+it.next()+"**"+" "); } %> <br> <a href=http://www.mamicode.com/"sponse.jsp">返回>聊天室
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。