小妹现从事网管界面开发,昨天刚编好的java小程序实现了命令行的socket通信,代码如下
服务器端(多线程)
import java.io.*;
import java.net.*;
class CreateServerThread extends Thread
{
private Socket client;
private BufferedReader in;
private PrintWriter out;
public CreateServerThread(Socket s) throws IOException
{
client = s;
System.out.println("--- Welcome ---");
start();
}
public void run()
{
try
{
in = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));
out = new PrintWriter(client.getOutputStream(), true);
String line = in.readLine();
if (!line.equals("bye"))
{
String msg = createMessage(line);
System.out.println(msg);
line = in.readLine();
}
else
System.out.println("--- See you, bye! ---");
//client.close();
}
catch (IOException e)
{}
}
private String createMessage(String line)
{
//String receive = line;
return line;
}
}
public class Server extends ServerSocket
{
private static final int SERVER_PORT = 10000;
public Server() throws IOException
{
super(SERVER_PORT);
try
{
while (true)
{
Socket socket = accept();
new CreateServerThread(socket);
}
}
catch (IOException e)
{}
finally
{
close();
}
}
//--- CreateServerThread
public static void main(String[] args) throws IOException
{
new Server();
}
}
客户端:
import java.io.*;
import java.net.*;
public class Client
{
Socket socket;
BufferedReader in;
PrintWriter out;
public Client()
{
try
{
socket = new Socket("127.0.0.1", 10000);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
out.println(line.readLine());
line.close();
out.close();
in.close();
socket.close();
}
catch (IOException e)
{}
}
public static void main(String[] args)
{
new Client();
}
}
这两个程序编译执行后可实现通信,即在客户端输入内容服务器端已可以接收。
现在的问题是如何将客户端的程序嵌入jsp网页中呢?
例如一个很简单的表单程序:
<!--
testBean
-->
<%@ page import = "ClientBean.ClientBean" contentType="text/html; charset=gb2312"%>
<jsp:useBean id="Client" class="ClientBean.ClientBean" scope="session" />
<jsp:setProperty name="in" property="*" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Client端测试</title>
</head>
<body>
<p>请在下面的表单中输入参数
</p>
<form name="form1" method="post" action="">
<p>
<input type="in" name="in">
</p>
<p>
<input type="submit" name="Submit" value="提交">
<input type="submit" name="Submit2" value="重置">
</p>
</form>
<p> </p>
</body>
</html>
如何使表单中输入的内容在服务器端正确显示出来呢?
我尝试使用JavaBean,还没试成功,但觉得方向是对的,向各位求助啦
谢谢!