Name: krT82822 Date: 02/20/2000
2/6/2000 eval1127@eng -- even with a 1.2.2 client, the messages arrive all at once. Is this just a matter of flushing a buffer at the servlet end?
(please see Comments section)
java version "1.3.0rc1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)
This might be a java.io issue.
My app sends a request to a servlet, the servlet responds every few seconds
with a status message, and then sends the replay. On 1.2.2 everything was
fine, but in 1.3 the whole output, including status messages, appears at once.
The following example has two parts, a server part and a client part. The
server part is a servlet for JSDK2.0. I run it on Apache, however, these seems
irrelevant since it works on all 1.2.2 clients.
The servlets sends 10 short message every two seconds after the connection has
been established.
The client piece (after the obvious adjustments) will contact the server and
should reads the messages when they are sent. Instead, all messages are read
on once.
I have not done extensive impact studies yet, but my guess is that on
transactions that task a long time, the client will timeout.
Servlet code
============
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public final class TestServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost (HttpServletRequest request, HttpServletResponse
response) throws ServletException {
try {
response.setContentType("text/html");
InputStream is = request.getInputStream();
byte[] buffer = new byte[5];
is.read(buffer);
System.out.println(new String(buffer));
OutputStream os = response.getOutputStream();
for (int i = 0; i < 10; i++) {
String text = "Test " + i;
os.write(text.getBytes());
os.flush();
Thread.sleep(2000);
}
is.close();
os.close();
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
client code
===========
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public final class ConnectDelay extends JFrame {
JTextField field = new JTextField(20);
JPanel panel;
public static void main(String argv[]) {
ConnectDelay app = new ConnectDelay();
}
public ConnectDelay() {
super("Connection Test");
panel = new JPanel();
panel.add(new JLabel("Message:"));
panel.add(field);
getContentPane().add(panel);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
show();
runTest();
}
public void runTest() {
try {
byte[] buffer = new byte[6];
URL url = new URL
("http://idgiot.dev.donnell.com/servlets/TestServlet");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(true);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
os.write("Hello".getBytes());
os.close();
InputStream is = connection.getInputStream();
int length = is.read(buffer);
while (length != -1) {
String text = new String(buffer);
field.setText(text);
System.out.println(text);
Rectangle rect = field.getBounds();
panel.paintImmediately
(rect.x,rect.y,rect.width,rect.height);
length = is.read(buffer,0,6);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
-----------------------
(Review ID: 100647)
======================================================================
- duplicates
-
JDK-4333920 getInputStream on URLConnection doesn't return until complete response arrives
-
- Resolved
-