javaweb聊天源码_java web 聊天

hacker|
81

文章目录:

用java实现web在线客服功能.不需要任何插件,用jsp和servlet如何实现。或者拿ssh框架。求源码或者详细思路

我使用过两种解决方案解决这个问题。

首先不能用插件,那我们想使用QQ啊,MSN之类的完成客服就行不通。

方案一就是采用网页聊天的形式。客服人员和客户分别在不同的网页上及时交流。使用Ajax技术,当某一方发出消息之后,通过js脚本监视该应用端页面的输入端,将发出的消息发送给服务器,服务器将消息处理之后发送给另一方,另一方接受到消息之后局部页面刷新,将消息显示出来。

方案二,做一个Applet小程序实现TCP/IP通信,将这个程序嵌入到网页中,用户需要客服的时候运行Applet小程序就可以了。

求JAVA webQQ在线聊天源码

客户端源代码

import java.net.Socket;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class socketClient

{

private DataOutputStream output;

private DataInputStream input;

private String clientName;

public static void main(String[] args)

{

//在main函数中,启动服务器的socket

new socketClient().ConnectServer();

}

public void ConnectServer()

{

try

{

Socket socket = new Socket("127.0.0.1",10001);

clientName = socket.getInetAddress().toString();

input = new DataInputStream(socket.getInputStream());

output = new DataOutputStream(socket.getOutputStream());

new readServer().start();

new writeServer().start();

}

catch(Exception e) {System.out.println(e.toString());}

}

public class readServer extends Thread

{

private Socket client;

public void run()

{

String msg;

try

{

while(true)

{

msg = input.readUTF();

if(msg!=null)

System.out.println("收到消息:【"+clientName+"】 "+msg);

}

}

catch(Exception e) {System.out.println(e.toString());}

}

}

public class writeServer extends Thread

{

private Socket client;

public void run()

{

try

{

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

String userInput;

while(true)

{

if(stdIn.ready())

{

userInput = stdIn.readLine();

if(userInput!="exit")

{

output.writeUTF(userInput);

System.out.println("已发送消息给【"+clientName+"】"+userInput);

}

}

}

}

catch(Exception e) {System.out.println(e.toString());}

}

}

}

服务器源代码

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class socketServer

{

ArrayList clientList = new ArrayList();

//private DataOutputStream output;

//private DataInputStream input;

//private String clientName;

//private Socket socket;

public static void main(String[] args)

{

//在main函数中,启动服务器的socket

new socketServer().OpenServer();

}

public void OpenServer()

{

try

{

ServerSocket server = new ServerSocket(10001);

Socket socket;

while((socket = server.accept())!=null)

{

clientList.add(socket);

//clientName = socket.getInetAddress().toString();

//output = new DataOutputStream(socket.getOutputStream());

//input = new DataInputStream(socket.getInputStream());

new readClient(socket).start();

new writeClient(socket).start();

}

}

catch(Exception e) {System.out.println(e.toString());}

}

public class readClient extends Thread

{

private Socket socket;

public readClient(Socket client)

{socket = client;}

public void run()

{

String msg;

try

{

String clientName = socket.getInetAddress().toString();

DataOutputStream output = new DataOutputStream(socket.getOutputStream());

DataInputStream input = new DataInputStream(socket.getInputStream());

while((msg = input.readUTF())!=null)

{

System.out.println("收到消息:【"+clientName+"】 "+msg);

}

}

catch(Exception e){System.out.println(e.toString());}

}

}

public class writeClient extends Thread

{

private Socket socket;

public writeClient(Socket client)

{socket = client;}

public void run()

{

try{

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

String userInput;

String clientName = socket.getInetAddress().toString();

DataOutputStream output = new DataOutputStream(socket.getOutputStream());

DataInputStream input = new DataInputStream(socket.getInputStream());

while(true)

{

if(stdIn.ready())

{

userInput = stdIn.readLine();

if(userInput!="exit")

{

output.writeUTF(userInput);

System.out.println("已发送消息给【"+clientName+"】"+userInput);

}

}

}

}

catch(Exception e) {System.out.println(e.toString());}

}

}

}

javaee项目“Web聊天室系统”的源码导入MyEclipse里,怎么配置数据库,tomcat,才能运行起来???

找到连接数据库的那个类(或者是属性文件),看看它用的是什么数据库。然后确定自己电脑上装了相应的数据库。

配置tomcat很简单,点击windows-preferences- 找到servers 然后选择tomcat对应的版本,我选的是6.X,然后按着提示找到tomcat的安装目录点击apply 确定就可以了

2条大神的评论

  • avatar
    访客 2022-07-12 上午 02:31:38

    putStream()); new readClient(socket).start(); new writeClient(socket).start(); } } catc

  • avatar
    访客 2022-07-11 下午 03:51:39

    y { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput;

发表评论