简单网页聊天室

最近在做一个java网络开发的一个小练习,简易网页聊天室:

1)是用oracle做数据库的,存有两张表

    a. chatuser表,保存登陆用户的

       SQL> desc chatuser
Name       Type         Nullable Default Comments
---------- ------------ -------- ------- --------
USERNAME   VARCHAR2(25)                          
LOGINTIME  TIMESTAMP(6) Y                        
LOGOFFTIME TIMESTAMP(6) Y                        
LOGINSTATE NUMBER(5)    Y

 

     b.UserChatContent表,保存聊天记录的

       SQL> desc UserChatContent
Name        Type           Nullable Default Comments
----------- -------------- -------- ------- --------
USERNAME    VARCHAR2(25)                            
CHATCONTENT VARCHAR2(1000) Y                        
SPEAKTIME   TIMESTAMP(6)   Y

 

jdbc工具包用了董工封装的dataSource

 

2)java类:

        a.entity:(5个)

             po类:ChatUser,UserChatContent   (对应数据库两张表)

             vo类:ChatUserVo,UserChatContentVo  (继承po类)

             其他:UserName  (用来查询数据库表时,只查用户名用的)

        b.logic:(2个)

              Checkpolice  (用来定时检查在线用户是否超时没操作的实现runnable借口的线程类)

              DaoChat      (处理与数据库的一系列操作,如:查询在线用户,登录,退出,查聊天记录等)

 

3)jsp页面:(5 个,我是放在意个chat3的文件夹里)

        a. login.jsp    登录页面,也处理重复登录,退出事务

        b. index.jsp    主页面,用frame框架,嵌套了聊天记录,在线用户,发言三大窗口

        c. content.jsp   显示当天聊天记录

        d. users.jsp     显示在线用户

        e. sayWordBox.jsp    发言窗口,退出按钮...

 

 

主要代码:

 

java类:

 

           entity类  5个       

 

-----------------ChatUser 类----------------------

 

package com.entity.po;

import java.sql.Timestamp;

public class ChatUser {
 private String userName;
 private Timestamp loginTime;
 private Timestamp logoffTime;
 private Integer loginState;//1为登录,0为未登录
 
 public ChatUser() {
  super();
 }
 public ChatUser(String userName) {
  super();
  this.userName = userName;
  
 }
 public ChatUser(String userName, Timestamp loginTime) {
  super();
  this.userName = userName;
  this.loginTime = loginTime;
 }
 public ChatUser(String userName, Timestamp loginTime, Timestamp logoffTime,
   Integer loginState) {
  super();
  this.userName = userName;
  this.loginTime = loginTime;
  this.logoffTime = logoffTime;
  this.loginState = loginState;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public Timestamp getLoginTime() {
  return loginTime;
 }
 public void setLoginTime(Timestamp loginTime) {
  this.loginTime = loginTime;
 }
 public Timestamp getLogoffTime() {
  return logoffTime;
 }
 public void setLogoffTime(Timestamp logoffTime) {
  this.logoffTime = logoffTime;
 }
 public Integer getLoginState() {
  return loginState;
 }
 public void setLoginState(Integer loginState) {
  this.loginState = loginState;
 }

}
------------------------end-------------------------

 

-----------------UserChatContent 类----------------------

 

package com.entity.po;

import java.sql.Timestamp;

public class UserChatContent {
 private String userName;
 private String chatContent;
 private Timestamp speakTime;
 
 public UserChatContent() {
  super();
 }
 
 public UserChatContent(String userName, String chatContent) {
  super();
  this.userName = userName;
  this.chatContent = chatContent;
  this.speakTime = new Timestamp( System.currentTimeMillis() );
 }

 public UserChatContent(String userName, String chatContent, Timestamp speakTime) {
  super();
  this.userName = userName;
  this.chatContent = chatContent;
  this.speakTime = speakTime;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getChatContent() {
  return chatContent;
 }
 public void setChatContent(String chatContent) {
  this.chatContent = chatContent;
 }
 public Timestamp getSpeakTime() {
  return speakTime;
 }
 public void setSpeakTime(Timestamp speakTime) {
  this.speakTime = speakTime;
 }

}

------------------------end-------------------------

 

-----------------ChatUserVo 类----------------------

 

package com.entity.vo;

import java.sql.Timestamp;

import com.entity.po.ChatUser;

public class ChatUserVo extends ChatUser{

 public ChatUserVo() {
  super();
  // TODO Auto-generated constructor stub
 }

 public ChatUserVo(String userName, Timestamp loginTime,
   Timestamp logoffTime, Integer loginState) {
  super(userName, loginTime, logoffTime, loginState);
  // TODO Auto-generated constructor stub
 }

 public ChatUserVo(String userName, Timestamp loginTime) {
  super(userName, loginTime);
  // TODO Auto-generated constructor stub
 }

 public ChatUserVo(String userName) {
  super(userName);
  // TODO Auto-generated constructor stub
 }

}

------------------------end-------------------------

 

 

-----------------UserChatContentVo 类----------------------

 

package com.entity.vo;

import java.sql.Timestamp;

import com.entity.po.UserChatContent;

public class UserChatContentVo extends UserChatContent{

 public UserChatContentVo() {
  super();
  // TODO Auto-generated constructor stub
 }

 public UserChatContentVo(String userName, String chatContent, Timestamp speakTime) {
  super(userName, chatContent, speakTime);
  // TODO Auto-generated constructor stub
 }

 public UserChatContentVo(String userName, String chatContent) {
  super(userName, chatContent);
  // TODO Auto-generated constructor stub
 }

}

------------------------end-------------------------

 

 

-----------------UserChatContent 类----------------------

 

 package com.entity;

public class UserName {
 private String userName;
 public UserName() {
  super();
 }
 public UserName(String userName) {
  super();
  this.userName = userName;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }

}

------------------------end-------------------------

 

 

        logic类   2个

 

-----------------DaoChat 类----------------------

 

package com.logic;

import java.sql.Timestamp;
import java.util.List;

import com.entity.UserName;
import com.entity.po.ChatUser;
import com.entity.po.UserChatContent;
import com.entity.vo.ChatUserVo;
import com.entity.vo.UserChatContentVo;
import com.sql.RemoteExecute;

public class DaoChat {
 
 
 @SuppressWarnings("unchecked")
 public boolean islogin(String userName){
  if( userName == null || "".equals(userName)){
   System.out.println("你传的参数不符合条件...");
   return false;
  }
  RemoteExecute remoteExecute = new RemoteExecute();
  String sql = "select userName from ChatUser where userName = '" + userName + "' and loginState = 1";
  List list = (List)remoteExecute.query(sql, null, UserName.class);
  closeRe(remoteExecute);
  if( list != null && list.size() != 0){
   return true;
  }
  return false;
  
 }
 
 
 @SuppressWarnings("unchecked")
 private boolean islogin(String userName,RemoteExecute remoteExecute){
  if( userName == null || "".equals(userName)){
   System.out.println("你传的参数不符合条件...");
   return false;
  }
  String sql = "select userName from ChatUser where userName = '" + userName + "' and loginState = 1";
  List list = (List)remoteExecute.query(sql, null, UserName.class);
  if( list != null && list.size() != 0){
   return true;
  }
  return false;
  
 }
 
 
 @SuppressWarnings({ "unchecked", "deprecation" })
 public boolean login(String userName){
  if( userName == null || "".equals(userName)){
   System.out.println("你传的参数不符合条件...");
   return false;
  }
  RemoteExecute remoteExecute = new RemoteExecute();
  String sql = "select * from ChatUser where userName = '" + userName + "'";//检查用户名是否已经登录过...如果登录过,则判断是否在线
  List list = (List)remoteExecute.query(sql, null, ChatUserVo.class);
  
  if( list != null && list.size() != 0){//已经登录过...
   if( list.get(0).getLoginState() == 1 ){//在线
    //System.out.println("用户列的登陆情况:" + list.get(0).getLoginState());
    closeRe(remoteExecute);
    return false;
   }else{//没在线
    Timestamp timestamp = new Timestamp( System.currentTimeMillis() );
    //(timestamp '2011-9-10 14:01:00')
    sql = "update ChatUser set loginState = 1, loginTime = (timestamp '" + timestamp.toLocaleString() + "') where userName = '" + userName + "'";
    remoteExecute.excute(sql, null);
    closeRe(remoteExecute);
    return true;
    
   }//else
  }else{//没登录过的,进行登录,并注册
   ChatUserVo user = new ChatUserVo();
   user.setUserName(userName);
   user.setLoginTime(new Timestamp( System.currentTimeMillis() ));
   user.setLoginState(1);
   remoteExecute.save(user, ChatUser.class, "userName");
   closeRe(remoteExecute);
   return true;
  }//else
  
 }//login
 
 
 private void closeRe(RemoteExecute remoteExecute) {
  if( remoteExecute == null )return;
  remoteExecute.commit();
  remoteExecute.close();
  remoteExecute = null;
 }
 
 
 public void checkLoginUsers() {
  CheckPolice checkPolice = new CheckPolice();
  Thread cheThread = new Thread(checkPolice,"checkPolice");
  cheThread.start();
 }
 
 
 
 @SuppressWarnings("deprecation")
 public boolean logoff(String userName){
  if( userName == null || "".equals(userName)){
   System.out.println("你传的参数不符合条件...");
   return false;
  }
  RemoteExecute remoteExecute = new RemoteExecute();
  Timestamp timestamp = new Timestamp( System.currentTimeMillis() );
  String sql = "update ChatUser set loginState = 0, logoffTime = (timestamp '" + timestamp.toLocaleString() + "') where userName = '" + userName + "'";
  int i = remoteExecute.excuteUpdate(sql, null);
  closeRe(remoteExecute);
  if( i >= 1 )
   return true;
  return false;
  
 }
 public boolean speak(String userName,String speakContent){
  if( userName == null || "".equals(userName)){
   System.out.println("你传的参数不符合条件...");
   return false;
  }
  RemoteExecute remoteExecute = new RemoteExecute();
  
  if( !this.islogin(userName, remoteExecute) ){
   return false;
  }
  
  UserChatContentVo userChatContentVo = new UserChatContentVo();
  userChatContentVo.setUserName(userName);
  userChatContentVo.setSpeakTime(new Timestamp( System.currentTimeMillis() ));

你可能感兴趣的:(javaWeb)