Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get database updates using servlets or jsp

What I want is to get database updates.

i.e If any changes occur to the database or a new record is inserted it should notify to the user.

Up to know what I implemented is using jQuery as shown below

 $(document).ready(function() {

     var updateInterval = setInterval(function() {

         $('#chat').load('Db.jsp?elect=<%=emesg%>');

     },1000);

 });   

It worked fine for me, but my teacher told to me that it's not a good way to do recommended using comet or long polling technology.

Can anyone give me examples for getting database updates using comet or long polling in servlets/jsp? I'm using Tomcat as server.

like image 567
user2976215 Avatar asked Nov 02 '22 11:11

user2976215


1 Answers

Just taking a shot in the dark since I don't know your exact environment... You could have the database trigger fire a call to a servlet each time a row is committed which would then run some code that looked like the following:

Get the script sessions that are active for the page that we want to update. This eliminates the need to check every reverse ajax script session that is running on the site. Once we have the script sessions we can use the second code block to take some data and update a table on the client side. All that the second code section does is send javascript to the client to be executed via the reverse ajax connection that is open.

String page = ServerContextFactory.get().getContextPath() + "/reverseajax/clock.html";
        Browser.withPage(page, new Runnable() {
            public void run() {
                Util.setValue("clockDisplay", output);
            }
        });



  // Creates a new Person bean.
                Person person = new Person(true);
                // Creates a multi-dimensional array, containing a row and the rows column data.
                String[][] data = {
                    {person.getId(), person.getName(), person.getAddress(), person.getAge()+"", person.isSuperhero()+""}
                };
                // Call DWR's util which adds rows into a table.  peopleTable is the id of the tbody and 
                // data conta

ins the row/column data.  
                Util.addRows("peopleTable", data);

Note that both of the above sections of code are pulled straight from the documentation examples @ http://directwebremoting.org/dwr-demo/. These are only simple examples of how reverse ajax can sent data to the client, but your exact situation seems to be more dependent on how you receive the notification than how you update the client screen.

Without some type of database notification to the java code I think you will have to poll the system at set intervals. You could make the system a little more efficient even when polling by verifying that there are reverse ajax script sessions active for the page before polling the database for info.

like image 186
constantlearner Avatar answered Nov 15 '22 05:11

constantlearner