wood burning stoves 2.0*
The moose likes JSP and the fly likes dynamically tail display of file contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "dynamically tail display of file contents" Watch "dynamically tail display of file contents" New topic
Author

dynamically tail display of file contents

Clarence Dyho
Greenhorn

Joined: Sep 17, 2001
Posts: 23
Hello,

Currently, my JSP calls an EJB service that returns a File object. I then display the contents of the file in the JSP. The file is just a regular text file generated by a logger. I have a refresh button that calls the same service and re-displays the latest contents of the file.

The specs were changed such that the users wants the refresh to be automatic (every 15 seconds, for example) but they do not want the overhead associated with calling the service, downloading the whole file, and displaying the contents. The display should be dynamic such that the updated portion will dynamically be "tailed" or appended to the existing contents in the JSP, much like the tail option when viewing files in Unix. The file could be several megabytes in size and they do not want to see the view going blank for a long time while the JSP tries to download and open the entire logfile.

My question is, is it possible to achieve this using pure JSP (and how) or should I be looking at something else such as Applets?

Thank you.

Regards,
Clarence
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

is it possible to achieve this using pure JSP


No. You will either need to resort to Applets, or use a combination JSP/Javascript approach.

Firstly. there is no pure JSP mechanism to cause a trigger at regular intervals -- you need a Javascript timer. You can't use a meta tag to cause the page to resubmit itself since that would cause the whole page to refresh and there would be no way to avoid resending the entire file contents on each round trip.

Here's one approach you could use:

1) Establish a Javascript timer to fire off a function at regular intervals. This function would cause a request to be submitted to an invisible iframe. In this way, you can communicate back to the server without causing the file content display to refresh.

2) The request would cause the backend to determine and retrieve the new content in the file (you'll need to maintain the file state in either the session or with parameters that get passed back and forth).

3) The response would return Javascript code that would cause the new file contents to be appended to the end of the currrent display using DOM manipulation.
[ October 08, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

First, it sounds like the whole idea of the EJB returning a File object isn't a really great design. A better idea might be to have the EJB completely hide the fact that it is logging to a file. When a new log message comes in, have the EJB log it to a file and save the message to a data structure that saves the last x log messages. Then, have the EJB return the last x log messages from the data structure when asked.

Also keep in mind that using Files and File IO in EJBs goes against the EJB spec. However, using them has worked in all the application servers I have used. Keep in mind that some of the higher facilities provided by the EJB spec can break systems relying on files or file IO (i.e. distributed servers, transaction processing, etc.)


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Clarence Dyho
Greenhorn

Joined: Sep 17, 2001
Posts: 23
Thank you Bear and Nathan.

Due to the complexity of the change invloved, priority is now on other critical items and the feature has been defered. I will revisit this again.
I think Javascript is going to play a bigger role this time, coupled with the way Nathan described the likely scenario.

Thank you for your help. More power!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

I like the level of abstraction that Nathan has proposed; when you revisit this, I concur that you should investigate his suggestions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamically tail display of file contents
 
Similar Threads
Refresh page programatically
Servlet reading a JSP file
Which Swing Component to Use to view log file ??
dynamical page content refreshing
Html question : href to a file : how to make it dynamic link?