码迷,mamicode.com
首页 > 其他好文 > 详细

聊天室

时间:2014-10-17 00:27:13      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:jsp   servlet   

此聊天室用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);
	}
}


登录界面

bubuko.com,布布扣

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="登录"></td>
            <td><input type="reset" value="取消"></td>
        </tr>   
  </form> 
  </center>
  </body>
</html>
聊天室界面

bubuko.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=display.jsp name=display frameborder="no" scrolling="auto">
   <frame src=sponse.jsp name=sponse frameborder="no">
  </frameset>
  <noframes></noframes>
</html>

上半部分的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="发送">
       <input type="reset" value="清除">
    </p>
    <p align="center">
       <a href="shangchuan.jsp">上传文件</a>
       <a href="userlist.jsp">查看在线用户</a>
       <input type="submit" value="清空聊天记录" onclick="qinkong()">
    </p>  
  </form>
  <%
    String user=(String)session.getAttribute("username");
    String father=(String)application.getAttribute("message");
    String son=request.getParameter("message");
    application.setAttribute("check", son);
    if(son!=null&&!son.equals("")&&user!=null&&father!=null){
       application.setAttribute("message",father+user+"说: "+son+"<br>");
    } 
  %>  
  </body>
</html>
上传文件页面

bubuko.com,布布扣

实现上传功能需要用到jspSmartUpload.jar包

bubuko.com,布布扣
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="good">
        <table border="0" align="center">
          <tr>
             <td><div align="center">
                 <input type="FILE" name="FILE1" size="30">
             </div></td>
          </tr>
          <tr>
             <td><div align="center">
                 <input type="submit" name="Submit" value="开始上传">
                 
                 <a href="display.jsp">返回</a>
             </div></td>
          </tr>
        </table>
     </form>
  </body>
</html>
上传一个shang.txt文件到工程目录mulu成功后的处理页面

bubuko.com,布布扣
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=mulu/"+fn+">mulu/"+fn+"文件</a>");
       }   
       catch(Exception ex)
       {
         ex.printStackTrace();
       }
    %>
    <br>
    <a href="sponse.jsp">返回</a>
  </body>
</html>
查看用户列表

bubuko.com,布布扣
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="sponse.jsp">返回</a>
  </center>
  </body>
</html>

聊天室

标签:jsp   servlet   

原文地址:http://blog.csdn.net/lindonglian/article/details/40158985

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!