2

I would like to display log file contents in a jsp page (as part of a web application functionality - cannot use another standalone webapp). I am looking for something similar to the unix tail -f functionality. I looked at Apache Commons Tailer and TailerListenerAdapter. However, the TailerListenerAdapter has a method handle(String line) which is not useful if you would like to see the output in a web browser. There are other solutions such as reading the file and then polling the file for new contents. But it would involve too many IO operations in a loop. Ideally I would want to tail the contents (say 2000 lines) and then display it in a jsp page. I could use an AJAX call, say ever 100 ms and refresh the data in the UI.

Anand
  • 1,791
  • 5
  • 23
  • 41
  • possible duplicate of [Java web application that can stream the content of an arbitrary file to the browser (live tail)](http://stackoverflow.com/questions/5803776/java-web-application-that-can-stream-the-content-of-an-arbitrary-file-to-the-bro) – DrLivingston Apr 18 '14 at 05:48
  • This could also be a duplicate of this: http://stackoverflow.com/questions/11345387/jsp-to-view-log-file-like-web-tail-f – DrLivingston Apr 18 '14 at 05:49

1 Answers1

0

You can keep the file stream open and keep reading from the position where you are at, either with non-blocking IO (so you don't block if there is no data) or you can check if the file has been modified before you read() using blocking IO.

If you don't want to keep the stream open, you can easily reopen it and skip() to the correct position.

nablex
  • 4,635
  • 4
  • 36
  • 51
  • If you keep reading you eventually hit eof and that was it then. You would at least need to use aRandomAccessFile. – Harald Apr 18 '14 at 06:24