大家进来讨论一下吧,关于Servlet的...
在Tomcat 下,我写了一个Servlet 如下:
功能是,不断从客户端读取数据,并显示:
public class UploadFileServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
InputStream inf=request.getInputStream();
int len=0;
byte b[]=new byte[20];
while((len=inf.read(b))>0)
{
String str=new String(b,0,len);
System.out.println(str);
}
}
}
我的客户端代码如下 :
URL url=new URL("http://localhost:8080/Test");//这个url可以访问上面的Servlet
HttpURLConnection httpConnection=(HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
OutputStreamWriter osw=new OutputStreamWriter(httpConnection.getOutputStream());
osw.write("A is Writing Something");
osw.flush();
System.in.read();//暂停给服务端输入数据...
osw.write("\n Are you OK?");
osw.flush();
osw.close();
System.out.println(httpConnection.getContentType());
那么按照客户端的流程,服务端首先应该将 A is Writing Something 输出来,
然后等待客户端敲入一个字符后,服务端接收到 后一行字符,即\n Are you OK? 并输出。
但实际情况下,为什么,在服务端,这两行字符都要等到 客户端敲入一个字符后,才同时一起输出来呢?