介绍:
本文的主要目的就是通过Java语言开发基于html通信的聊天程序,主要用到了ajax,html通信,java, jsp。穿越防火墙,在web页面进行通信,没有利用socket,不用另外开设端口进行通信。仅仅利用浏览器的80端口就可以搞定。
下面列出主要的代码结构:
下面给出涉及到的各个代码:
1.ChatService.java类:
package lee;
import java.util.*;
import java.io.*;
/*
* @version 1.0
*/
public class ChatService
{
private static ChatService cs;
private Properties userList;
private LinkedList chatMsg;
private ChatService()
{
}
public static ChatService instance()
{
if (cs == null)
{
cs = new ChatService();
}
return cs;
}
public boolean validLogin(String user , String pass)
throws IOException
{
if (loadUser().getProperty(user) == null)
{
return false;
}
if (loadUser().getProperty(user).equals(pass))
{
return true;
}
return false;
}
public boolean addUser(String name , String pass)
throws Exception
{
if (userList == null)
{
userList = loadUser();
}
if (userList.containsKey(name))
{
throw new Exception("用户名已经存在,请重新选择用户名");
}
userList.setProperty(name , pass);
saveUserList();
return true;
}
public String getMsg()
{
if (chatMsg == null)
{
chatMsg = new LinkedList();
return "";
}
String result = "";
for (String tmp : chatMsg)
{
result += tmp + "\n";
}
return result;
}
public void addMsg(String user , String msg)
{
if (chatMsg == null)
{
chatMsg = new LinkedList();
}
if (chatMsg.size() > 40)
{
chatMsg.removeFirst();
}
chatMsg .add(user + "说:" + msg);
}
//
// 下面是系统的工具方法