So, I'm trying to build up my knowledge of web, JSON, Javascript fetch, promises, etc., and I'm hating life.
I keep getting either an AbortError from the JS fetch call or a connection reset from a direct connect to http://localhost:8080 in the browser. I've tried as "text/plain" as well as removing extra newlines, calling flush() on the output stream.
I'm guessing that the socket is getting closed AFTER the information is pushed into the output stream, but BEFORE the browser is able to absorb it all.
How do I "wait", in Java, for the other end of a socket to process all the data in the pipe? Or is that even the problem I'm running into? (Note: I've not had this trouble before with the socket programming I've done before. I've got one process where a client connects and I spew tons of data down the pipe with none of this malarky.)
Am I missing something obvious? Yes, I know, I haven't really implemented a web server here, but it should work, shouldn't it?
(Note: (1) I'm just trying to get something to work, (2) I don't have access to any and all libraries on the 'net due to my constricted environment, so I can't just try Jetty, etc.)
I keep getting either an AbortError from the JS fetch call or a connection reset from a direct connect to http://localhost:8080 in the browser. I've tried as "text/plain" as well as removing extra newlines, calling flush() on the output stream.
I'm guessing that the socket is getting closed AFTER the information is pushed into the output stream, but BEFORE the browser is able to absorb it all.
How do I "wait", in Java, for the other end of a socket to process all the data in the pipe? Or is that even the problem I'm running into? (Note: I've not had this trouble before with the socket programming I've done before. I've got one process where a client connects and I spew tons of data down the pipe with none of this malarky.)
Am I missing something obvious? Yes, I know, I haven't really implemented a web server here, but it should work, shouldn't it?
(Note: (1) I'm just trying to get something to work, (2) I don't have access to any and all libraries on the 'net due to my constricted environment, so I can't just try Jetty, etc.)
The "Server":
Code:
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
ServerSocket server = new ServerSocket(8080);
while(true) {
Socket client = server.accept();
Thread t = new Thread(new Runnable() {
public void run() {
try {
handleRequest(client);
} catch (Exception e) {
LOG.log(Level.SEVERE, "", e);
}
}
});
t.start();
}
}
private static void handleRequest(Socket client) throws SQLException, IOException {
try (Connection conn = DriverManager.getConnection("", "", "") {
try (PreparedStatement ps = conn.prepareStatement(P_QUERY)) {
try (ResultSet rs = ps.executeQuery()) {
JSONArray ja = new JSONArray();
while(rs.next()) {
Item item = itemFromRs(rs);
JSONObject jo = toJsonObject(item);
ja.put(jo);
}
String output = ja.toString();
try (PrintWriter pw = new PrintWriter(client.getOutputStream())) {
pw.println("HTTP/1.1 200 OK");
pw.println("Access-Control-Allow-Origin: *");
pw.println("Content-Type: application/json"); // have tried as text/plain
pw.println("Content-Length: " + output.length());
pw.println();
pw.println(output);
pw.println(); // tried with/without this
pw.flush(); // tried with/without this
}
}
}
}
}
}