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