Refresh GUI on XML update without restarting Tomcat(Server)
Jul 20th, 2009 | By admin | Category: ServletsI am writing this post on the basis of my learning on refreshing UI(User interface) without restarting server(Tomcat , in my case) on update of an XML which resides inside the container. Often we follow the design where we write our application configuration into an XML file and read – write the XML based on the requirements.
lets take an example of an application which supports user management and keeps track of the users accessed by Admin in a History.Requirement is to persist the history information in such a manner so that it should be accessible well after application restart or server restart.So XML could be one of the option where you can write the history info and read it back whenever required.If the XML is to be placed in your war file , it has to be under web/ or web/
Just think about a situation where an admin had accessed 100 users and your code to write to XML executed perfectly to write to the XML file under web content directory.If admin wish to see the history browsing to the history page , he would not be able to see the latest history without restarting the server and the application.You would have written to the XML file successfully but to have the effect of getting the updated(recently written) value , server must be restarted. This is time consuming and none of web application can wait for it.
Solution of this problem is to write the history details in a Collection(it can be a list , map , array) or in a string. populate your GUI(History page) from the collection (of history) or the String. Write the collection to the XML file on log-out from the application.
Lets discuss in a step by step manner.
Step 1 : Put(or insert) all the History object in a collection
Step 2 : For each action listener , if the history is modified ( added or deleted) , modify the History collection.
Step 3 : Make your GUI to read the details(History details) from the collection , not from the history directly.
Step 4 : Write to the XML file , on log-out from the application
Step 5 : Have mechanism to read the XML and populate the Collection on log-in to the application.
Lets have a look at the following picture.

Flow Diagram
The each step can be described as,
1 . The application GUI uses a program logic on log-in to the application.
2 . The logic reads the XML file
3 . Populate the collection for future use of populating the GUI.
4. Read the History from the collection not from the UI directly.
5. If there is any event triggered from the GUI (say , add to history) , do not add to the XML directly.
6. Add to the Collection using the logic.
7. Add to the XML file while log-out.
This way , you can have your GUI rendering the recent data and can sync the XML file on log-out. There must be different ways to deal with this. Reader of this post is expected to share if any other ways are known.
