简体   繁体   中英

Java tail log file and display it in jsp

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM