Is it possible to write text into a web page after it has loaded?

user9841626 :

Let me get straight to an example to explain further.

final var socket = new java.net.ServerSocket(1234);
for (;;)
{
     try (final var client = socket.accept())
     {
          client.getOutputStream().write("HTTP/1.1 200 OK\r\n\r\n".concat(java.time.Instant.now().toString()).getBytes());
     }
}

When I now open my browser of choice (Firefox cough) I'll receive the current time and date. The question now is how I can write to that socket at a later point in time.

hypothetical solution

Here's something I already tried, but doesn't work at all.

final var socket = new java.net.ServerSocket(1234);
    for (;;)
    {
         try (final var client = socket.accept())
         {
              client.getOutputStream().write("HTTP/1.1 200 OK\r\n\r\n".concat(java.time.Instant.now().toString()).getBytes());
              client.getOutputStream().flush();
              Thread.sleep(1000L);
              client.getOutputStream().write("And another paragraph.".getBytes());
         } 
    }

The result is a web page loading for approximately a single second, printing out the following result (may vary due to different date and time on your end).

2019-01-19T18:19:15.607192500Z
And another paragraph.

Instead I would like the see something like that:

  1. print out the current time and date.
  2. wait a second without the content of the web page changing.
  3. print out the next paragraph.

How would I go about implementing that?

gutch :

Is it possible for the server to write text into a web page after it is loaded? Yes it definitely is, but these days I suspect it it is rarely done. I started web development in the 1990s and back then that was a pretty common technique. We used it to write live chat messages to browsers with no Javascript. These days Javascript is ubiquitous and powerful, so using client-side Javascript to update a page will be the best option in most cases.

That said, the technologies we used for writing server-side updates back then should still work now. I suspect the reason you don't see updates in your browser is because it doesn't know it should start displaying the page before everything is loaded. Using chunked transfer encoding, a 1990s technology still supported by modern browsers, should resolve that. It it allows the server to indicate when a 'chunk' of data is complete and browsers will generally process each chunk immediately rather than wait for all the chunks to arrive.

The easiest way to use chunked transfer encoding is to use an HTTP library like Apache HttpComponents, then wrap your output stream in the appropriate class:

final var socket = new java.net.ServerSocket(1234);
for (;;)
{
  try (final var client = socket.accept())
  {
    var outputStream = new ChunkedOutputStream(client.getOutputStream());
    outputStream.write("HTTP/1.1 200 OK\r\n\r\n".concat(java.time.Instant.now().toString()).getBytes());
    outputStream.flush();
    Thread.sleep(1000L);
    outputStream.write("And another paragraph.".getBytes());
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=105907&siteId=1