applet和servlet通信的问题
我想在applet中用线程来一直和servlet保持连接,这样做行不行?
我认为servlet是由请求来激活的吧。
applet中的run()如下:
public void run() {
try {
URL url = new URL("http://localhost:8080/examples/servlet/ComServServlet");
URLConnection ucon = url.openConnection();
ucon.setDoInput(true);
ucon.setUseCaches(false);
DataInputStream dis = new DataInputStream( ucon.getInputStream());
String result = dis.readUTF();
dis.close();
} catch ( Exception e){
stop();
}
}
servlet中的service()如下:
public void service(
HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
res.setContentType( "application/octet-stream");
DataOutputStream dos = new DataOutputStream( res.getOutputStream());
for(int i=0; i<10; i++){
try{
dos.writeUTF( "★" + i);
Thread.sleep(1000);
}catch(Exception e){
dos.writeUTF("☆"+e.getMessage());
break;
}
}
dos.flush();
dos.close();
}
请各位高手指点一二,因为得到结果一直都是:★0,
我想知道为什么在网页上,不是隔一秒显示的是i+1后的值。