Linux中国  设为主页
 收藏本站
 
当前位置: > 首页 ->Linux技术 ->系统管理 ->JSP专题:第六部分:用JSP实现聊天室-2
  相关分类: 
入门与提高
系统管理
网络应用
嵌入式系统
内核研究
服务器相关
发行版专区
Linux程序设计
Linux安全
BSD相关
桌面应用
  站内搜索: 
热门文章排行
热门文章排行 Adaptec 2100S RAID卡安装快速指南 (05-05)
JSP专题:第六部分:用JSP实现聊天室(05-05)
Linux 99问(一)(05-05)
Linux系统中网络配置详解(05-05)
JSP专题:第六部分:用JSP实现聊天室(05-05)
精采文章排行
精采文章排行 Motorola微处理器bootloader分析与应(06-04)
Linux系统:让内存不再泄漏的实用技(06-04)
Fedora Core5 NFS服务器搭建过程介绍(06-04)
新手看招 手把手教你安装VMware虚拟(06-04)
“侵权事件” 红帽称微软企图干扰用(06-04)
 

JSP专题:第六部分:用JSP实现聊天室-2

作者:Webmaster   来源:Linux-cn.com   点击:   日期:2007-05-05 [收藏] [投稿]

  IE是否经常中毒?推荐您

四、聊天室主程序

4.1一个保存用户单条发言的类


speaking.java:
package customers;
import java.util.*;
public class speaking 
  {String mySpeaking;/*发言内容*/
   String sourceUserId;/*发出信息的用户的用户ID*/
   String targetUserId;/*接收信息的用户的用户ID*/
   String sourceUserName;/*发出信息的用户的用户名*/
   String targetUserName;/*接收信息的用户的用户名*/
   String face;/*表情*/
   String color;/*显示颜色*/
   String radprivate;/*是否私聊*/
   public void setSpeaking(String theSpeaking,String theSourceUserId,
   String theTargetUserId,
      String theSourceUserName,String theTargetUserName,
      String theFace,String theColor,
      String theRadprivate)
   //新发言内容设定
     {mySpeaking=theSpeaking;
      sourceUserId=theSourceUserId;
      targetUserId=theTargetUserId;
      sourceUserName=theSourceUserName;
      targetUserName=theTargetUserName;
      face=theFace;
      color=theColor;
      radprivate=theRadprivate;
     }
   public String returnSpeaking(String thisUserId)
   //返回某一用户所能看到的该发言内容显示
     {String thisSpeaking=new String("");
      if (radprivate.equals("system")) 
         {if (color.equals("red"))
             thisSpeaking="<font size=2 color=red>(系统公告)"
	     +sourceUserName+"进入聊天室。</font><br>";
          else 
             thisSpeaking="<font size=2 color=green>(系统公告)"
	     +sourceUserName+"离开聊天室。</font><br>";
         }
         //约定是否私聊为“system”,显示颜色为“red”及“green”
	 时分别表示用户进出聊天室的系统公告,该信息对所有用户可见
      else
       {if ((!radprivate.equals("yes"))' '(targetUserId.equals(thisUserId))' '
             (sourceUserId.equals(thisUserId)))
             {if (targetUserId.equals("all"))
                 thisSpeaking="<font size=2 color="+color+">"
		 +sourceUserName+"对大家"+returnFace(face)+"说:
		 "+mySpeaking+"</font><br>";
              else 
                 thisSpeaking="<font size=2 color="+color+">"
		 +sourceUserName+"对"+targetUserName+returnFace(face)+"说:
		 "+mySpeaking+"</font><br>";
             }
         }
         //约定非私聊信息、该用户发出的信息、向该用户发出的信息、
	 向大家发出的信息,具备以上条件之一的聊天信息对该用户可见。
      return thisSpeaking; 
     }
  public String returnFace(String theFace)
  //根据表情代码返回表示表情的字符串
     {int nn;
      nn=Integer.parseInt(theFace);
      String thisFace=new String("");
      switch (nn)
         {case 1:thisFace="笑嘻嘻地";break;
          case 2:thisFace="微笑着";break;
         } 
      return thisFace;
     }
 }
 

  又是类,你应该看到,在Java中我们与类的接触是如此频繁,正如我们在前面提到的,在Java看来:世间万物都是类。当然,我相信,作为一个有经验的程序员,即使是在Java中,不使用类你同样可以编出功能完备的程序。但是,不论你以前对面向对象的编程怎么看,在Java中,你必须习惯这种用法。而且,从我个人而言,我觉得类是一个蛮可爱的东西。

  另外,到目前为止,我们已经多次使用到String类型的变量。在Java中变量的情况同样符合类的概念,除了为数不多的诸如int这样的简单类型变量外,绝大多数的变量都是类。而String也是一个类,对于String有它自己的许多方法,你不能用处理简单类型变量的格式来处理它,例如:if (color.equals("red")你就不能写成if (color=="red"),有一种情况例外,当你判断一个String变量是否为空值时,你可以使用if (color==null)。

4.2保存聊天室信息的类

·Fedora Core5 NFS服务器搭建过程介绍 ·Linux系统:让内存不再泄漏的实用技巧 ·新手看招 手把手教你安装VMware虚拟机 ·“侵权事件” 红帽称微软企图干扰用户 ·删除Linux后 如何找回Windows启动菜单 ·菜鸟乐园 Linux中常见文件系统格式介绍 ·Linux操作系统下IPTables配置方法详解 ·实用技巧 Linux系统的经典使用技巧八则 ·Linux系统文件优化及磁盘检查方法介绍

ChatRoom.java:
package customers;
import java.util.*;
public class ChatRoom 
  {speaking chatSpeaking[][];/*聊天室的全部发言内容*/
   private int speakingCount[];/*聊天室的发言条数*/
   private int userCount[];/*聊天室的在线人数*/
   oneUser theUserList[][];/*聊天室的在线用户列表*/
   public ChatRoom() 
   //构造函数
     {theUserList=new oneUser[3][100];
      userCount=new int[3];
      chatSpeaking=new speaking[3][50];
      speakingCount=new int[3];
      userCount[1]=0;
      userCount[2]=0;
      speakingCount[1]=0;
      speakingCount[2]=0;
     }
   public void addUserList(String theOneUserId,String theOneUserName,
   long theOneUserTime,String theOneUserPosion,String theOneUserSwjg,
   String theOneUserSfks,int chatroom)
   //向在线用户列表中添加用户,如果用户已存在,将不重复添加
     {int i;
      for (i=1;i<=userCount[chatroom];i++) if (returnUserId(chatroom,i)
      .equals(theOneUserId)) break;
      if (i==userCount[chatroom]+1)
        {userCount[chatroom]=userCount[chatroom]+1;
         oneUser theOneUser=new oneUser();
         theOneUser.setOneUser(theOneUserId,theOneUserName,theOneUserTime,
	 theOneUserPosion,theOneUserSwjg,theOneUserSfks);
         theUserList[chatroom][userCount[chatroom]]=theOneUser;
        }
     }
   public void deleteUserList(String theOneUserId,int chatroom)
   //从在线用户列表中删除用户
     {int i;
      for (i=1;i<=userCount[chatroom];i++) if (returnUserId(chatroom,i)
      .equals(theOneUserId)) 
       theUserList[chatroom][i]=theUserList[chatroom][userCount[chatroom]];
      userCount[chatroom]=userCount[chatroom]-1;
     }
   public void addSpeaking(String theSpeaking,String theSourceUserId,
   String theTargetUserId,String theSourceUserName,String theTargetUserName,
   String theFace,String theColor,String theRadprivate,int chatroom)
   //增加聊天信息,如果发言条数未超过30条,则添加一条,发言条数相应加1;
   反之,将丢弃最先的一条发言,再将新发言添加到最后,发言条数不变
     {speaking thisSpeaking=new speaking();
thisSpeaking.setSpeaking(theSpeaking,theSourceUserId,theTargetUserId,
theSourceUserName,theTargetUserName,theFace,theColor,theRadprivate);
      if (speakingCount[chatroom]<=30)
         {speakingCount[chatroom]=speakingCount[chatroom]+1;
          chatSpeaking[chatroom][speakingCount[chatroom]]=thisSpeaking;
         }
      else
         {int i;
          for (i=1;i<speakingCount[chatroom];i++) chatSpeaking[chatroom][i]
chatSpeaking[chatroom][i+1];
          chatSpeaking[chatroom][speakingCount[chatroom]]=thisSpeaking;
         }
     }
   public void setUserTime(String theUserId,long theUserTime,int intchatroom)
   //设置用户时间
     {int i;
      for (i=1;i<=userCount[intchatroom];i++) if ((theUserList[intchatroom][i]
      .getUserId()).equals(theUserId))
         {theUserList[intchatroom][i].setUserTime(theUserTime);
          break;
         }
     }
   public int returnUserCount(int chatroom)
   //获取在线人数
     {return userCount[chatroom];
     }
   public int returnSpeakingCount(int chatroom)
   //获取发言条数
     {return speakingCount[chatroom];
     }
   public String returnUserId(int chatroom,int i)
   //获取聊天室中指定序号的用户的用户ID
     {return theUserList[chatroom][i].getUserId();
     }
   public String returnUserName(int chatroom,int i)
   //获取聊天室中指定序号的用户的用户名
     {return theUserList[chatroom][i].getUserName();
     }
   public String returnUserName(int chatroom,String userId)
   //获取聊天室中指定用户ID的用户的用户名
     {String UserName=new String("");
      int i;
      for (i=1;i<=userCount[chatroom];i++) 
         if (returnUserId(chatroom,i).equals(userId))
            UserName=theUserList[chatroom][i].getUserName();
      return UserName;
     }
   public String returnAllSpeaking(int chatroom,String thisUser)
   //获取某用户可见的全部聊天信息
     {int i;
      java.util.Date dateTimenow=new java.util.Date();
      long longDateTimeNow=(long)dateTimenow.getTime();
      setUserTime(thisUser,longDateTimeNow,chatroom);
      for (i=1;i<=userCount[chatroom];i++)
        {long oneUserTime=(long)theUserList[chatroom][i].getUserTime();
         if (longDateTimeNow - oneUserTime>60000)
{addSpeaking("","","",theUserList[chatroom][i].getUserName(),"","","green",
"system",chatroom);
             deleteUserList(theUserList[chatroom][i].getUserId(),chatroom);
             //在调用该方法时,同时判断有无超时用户,如有,将其从在线用户列
	     表中自动删除,并发出相应系统公告
            }
        }
      String allSpeaking=new String("");
      for (i=1;i<=speakingCount[chatroom];i++)
      allSpeaking=allSpeaking+chatSpeaking[chatroom][i]
      .returnSpeaking(thisUser);
      return allSpeaking;
     }
  } 


 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<

上一页12 3 4 5 6 下一页

上一篇:JSP专题:第六部分:用JSP实现聊天室-1   下一篇:不同Web主机上的Servlet之间数据对象的相互传输
文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论

   相关文章:
·Motorola微处理器bootloader分析与应用

   文章评论:(1条)
  
 请留名: 匿名评论   点击查看所有评论 论坛讨论
 

 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。