急急急,在线等Java编程高手来,在聊天室中如何实现发送消息和显示聊天消息内容,我只写了框架,如下 170

我是初学者,最好能写出详细代码,谢谢<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%S... 我是初学者,最好能写出详细代码,谢谢
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>聊天系统</title>
<style type="text/css">
</style>
</head>

<body>
<div class="main">
<div class="top">
<form action="" name="top" method="post">
<table>
<tr>
<br>
<br>
<td>
<h1>
欢迎进入聊天系统
</h1>
</td>
</tr>
</table>
</form>
</div>
<div class="left">
<form action="" name="left" method="post">
<table>
<tr>
<td>
<a>当前用户:</a>
</td>
</tr>
<tr>
<td>
<a>ID:</a>
</td>
</tr>
<tr>
<td>
<a href="login.jsp">注销登录</a>
</td>
</tr>
</table>
</form>
</div>
<div class="right">
<form action="" name="right" method="post">
<table>
<tr>
<td>
在线用户列表:
</td>
</tr>
</table>
</form>
</div>
<div class="center">
<form action="" name="center" method="post">
<table border="1" bordercolor="red" cellspacing="2" cellpadding="2"
width="400px;">
<tr>
<td>
聊天信息
</td>
</tr>
<tr>
<td><textarea name="content" readonly="readonly" cols="45" rows="10"></textarea></td>
</tr>
<tr>
<td>
<select>
<option>
字体
</option>
<OPTION VALUE="楷书_GB2312">楷书_GB2312</OPTION>
<OPTION VALUE="宋体" SELECTED>宋体</OPTION>
<OPTION VALUE="楷体_GB2312">楷体</OPTION>
<OPTION VALUE="隶书">隶书</OPTION>
<OPTION VALUE="黑体">黑体</OPTION>
<OPTION VALUE="华文行楷">华文行楷</OPTION>

</select>
<select>
<option>
字体号
</option> </select>
<tr>
<td>
<textarea name="content" cols="45" rows="6"></textarea>
</td>
</tr>
<tr align="center">
<td colspan="2">
<code=JSP>
<%String toSelect=request.getParameter("toSelect"); %> <input name="toSelect" type="text" id="toSelect" value=" <%=(toSelect==null?"所有人":toSelect)%>" readonly>
</code><input type="submit" name="session" value="发送"><input type="reset" name="cancel" value="取消">
</td></tr></table></form></div></div></body></html>
我写了的啊????不知道怎么去实现哦,急急急急急急急急急急急
展开
 我来答
antasports
2010-09-09 · TA获得超过1164个赞
知道小有建树答主
回答量:235
采纳率:0%
帮助的人:96.4万
展开全部
你这根本什么都没写啊,原来写了个模拟QQ的,不过没去画界面,供你参考下,希望能帮到你,主要的是你把原理弄清楚就应该没问题了
服务器:
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.*;
public class Server
{
/**
* @param args
*/
private int count = 0;
private int num = 0;
HashMap<Integer,Socket> clients = new HashMap<Integer,Socket>();
public Server()
{

try
{
ServerSocket server = new ServerSocket(33333);
while(true)
{
Socket fromClient = server.accept();
count++;
num++;
clients.put(count, fromClient);
new Thread()
{
public void run()
{
receive();
}
}.start();
new Thread()
{
public void run()
{
send();
}
}.start();
}
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void receive()
{
try
{
BufferedReader read = new BufferedReader(new InputStreamReader(clients.get(count).getInputStream()));
int tmp = count;
int temp = 0;
while(true)
{
String value = "";
try
{
value = read.readLine();
String regex = "^ *~ *(\\d+) *:(.*)";
Pattern pat = Pattern.compile(regex);
Matcher match = pat.matcher(value);
if(match.find())
{
temp = Integer.valueOf(match.group(1));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(clients.get(temp).getOutputStream()));
write.write("用户"+tmp+":"+match.group(2)+"\n");
write.flush();
}
else
{
for(int i = 1;i<=num;i++)
{
if(i == tmp)
{
continue;
}
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(clients.get(i).getOutputStream()));
write.write("用户"+tmp+":"+value + "\n");
write.flush();
}
}
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch(IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

public void send()
{
Scanner scan = new Scanner(System.in);
String tmp = "";
while(!tmp.equals("exit"))
{
tmp = scan.nextLine();
try
{
for(int i = 1;i<=num;i++)
{
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(clients.get(i).getOutputStream()));
write.write("系统:"+tmp + "\n");
write.flush();
}
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static void main(String[] args)
{
// TODO Auto-generated method stub
new Server();
}
}

客户端:
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client
{
/**
* @param args
*/
BufferedReader read;
BufferedWriter write;
public Client()
{
try
{
Socket client = new Socket("192.168.1.7",33333);

read = new BufferedReader(new InputStreamReader(client.getInputStream()));
write = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
new Thread()
{
public void run()
{
recieve();
}
}.start();

new Thread()
{
public void run()
{
send();
}
}.start();
//send();
}
catch(UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void send()
{
Scanner scan = new Scanner(System.in);
String tmp = "";
while(!tmp.equals("exit"))
{
tmp = scan.nextLine();
try
{
tmp= tmp+"\n";
write.write(tmp);
write.flush();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

void recieve()
{
while(true)
{
try
{
String value = "";
value = read.readLine();
System.out.println(value);
write.flush();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public static void main(String[] args)
{
// TODO Auto-generated method stub
new Client();
}
}
5iijava
2010-09-08 · TA获得超过190个赞
知道答主
回答量:165
采纳率:0%
帮助的人:93.8万
展开全部
要想显示聊天内容的话两种方法。
1、不停地刷新页面
2、用ajax
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jackcandyhouse
2010-09-06
知道答主
回答量:10
采纳率:0%
帮助的人:0
展开全部
对于 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> ,怎么没有设置contentType?这可是很重要的呀!!!!

这个问题很复杂,还差参考网上的源代码吧。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友7a78332
2010-09-09 · 超过10用户采纳过TA的回答
知道答主
回答量:180
采纳率:0%
帮助的人:29.4万
展开全部
晚上给你一个。我原来整过。加我987463535,;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式