[b]1、 工程说明[/b]
java project——CometSocket包含一个类:
Calculate.java:作为客户端,往服务器发送数据
web project——CometServer包含:
ReceiverServlet.java:接收客户端数据,并模拟入库(以存入文件的形式)
TestServlet.java:模拟从库中取数据(从文件中取值)并推送给浏览器展示
index.jsp:接收从服务端推送出来的数据并展示
[b]2、部分代码说明[/b]
[b][color=darkred]Calculate.java[/color][/b]
通过http get请求方式将数据发送给客户端
[color=darkred][b]ReceiverServlet.java [/b][/color]
接收数据并模拟入库
[color=darkred][b]TestServlet.java[/b][/color]
定时向服务器推送数据
[b]3、运行 [/b]
[color=indigo] tomcat配置:[/color] 要使用 Comet,需要对 Tomcat 的配置文件 server.xml 稍作修改。默认情况下启用的是更典型的同步 IO 连接器。现在只需将它切换成异步版本.
<!-- Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" [color=red]protocol="HTTP/1.1" [/color]redirectPort="8443"/ -->
<Connector connectionTimeout="20000" port="8080" [color=red]protocol="org.apache.
coyote.http11.Http11NioProtocol"[/color] redirectPort="8443"/>
源码和效果图,如附件:
启动web工程,点击“start”
运行Calculate.java
java project——CometSocket包含一个类:
Calculate.java:作为客户端,往服务器发送数据
web project——CometServer包含:
ReceiverServlet.java:接收客户端数据,并模拟入库(以存入文件的形式)
TestServlet.java:模拟从库中取数据(从文件中取值)并推送给浏览器展示
index.jsp:接收从服务端推送出来的数据并展示
[b]2、部分代码说明[/b]
[b][color=darkred]Calculate.java[/color][/b]
public void sendData(ActionEvent e)
{
try
{
//发送数据
txtResult.setText(e.getActionCommand());
URL url = new URL("http://localhost:8080/CometServer/rs?num="+txtResult.getText());
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestProperty("Content-Type", "text/html"); //application/xml
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(2*1000);
int code = httpURLConnection.getResponseCode();
System.out.println("code_______________"+code);
}
通过http get请求方式将数据发送给客户端
[color=darkred][b]ReceiverServlet.java [/b][/color]
public void service(HttpServletRequest request ,HttpServletResponse response){
FileOutputStream fos = null;
try
{
String str = request.getParameter("num");
System.out.println("get________________________________"+str);
File file = new File("D:/1.txt");
fos = new FileOutputStream(file);
if(str != null){
fos.write(str.getBytes());
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fos != null){
try
{
fos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
接收数据并模拟入库
[color=darkred][b]TestServlet.java[/b][/color]
public void event(CometEvent event) throws IOException, ServletException
{
// 开始
if(event.getEventType() == EventType.BEGIN){
try
{
Thread.sleep(100);
File file = new File ("D:/1.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = br.readLine();
System.out.println("ddd1_________"+str);
if( str != null){
HttpServletResponse response = event.getHttpServletResponse();
PrintWriter out = response.getWriter();
out.write(str);
out.flush();
out.close();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(event.getEventType() == EventType.END){
System.out.println("ddd2");
event.close();
}
}
定时向服务器推送数据
[b]3、运行 [/b]
[color=indigo] tomcat配置:[/color] 要使用 Comet,需要对 Tomcat 的配置文件 server.xml 稍作修改。默认情况下启用的是更典型的同步 IO 连接器。现在只需将它切换成异步版本.
<!-- Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" [color=red]protocol="HTTP/1.1" [/color]redirectPort="8443"/ -->
<Connector connectionTimeout="20000" port="8080" [color=red]protocol="org.apache.
coyote.http11.Http11NioProtocol"[/color] redirectPort="8443"/>
源码和效果图,如附件:
启动web工程,点击“start”
运行Calculate.java