views:

74

answers:

3

I want to build realtime application using java. The web application always request data to server every 10 seconds, and display the fresh data on the web page.

do you have idea how to solve case like this ?

+3  A: 

Use ajax to query the server every 10 seconds. You can easily achieve this by using jquery in the frontend.

Your backend don't need anything special but a good idea is use web service which will return an XML or JSON that will be parsed by the front end jquery to update the display.

mezzie
+2  A: 

Ajax will make your JavaScript code poll your site server to get the last value of the currency, say every 10 seconds. Meaning if 100 persons are connected, you'll get on average 100 accesses in 10 seconds.

Unless you use a Flash object or a Java Applet to establish a TCP-IP connection with your server (that can push the new value when it is available), Ajax is a better/easier option for you. The TCP alternative provides faster results (clients see the new value more in real time than with Ajax polling), and, usually, more efficient in terms of performance (Only push when a new value is available).

If you implement an Ajax polling system, you'll have to add server side an abuse detector: many people understand well JavaScript and some of them may change the polling frequency to have newer values faster... (like every second). Depends on the audience, the number of people accessing your site etc...
That detector would ensure that a given client does not exceed the polling frequency (e.g. more than once every 12 seconds, if the frequency is 10, with an error margin)

ring0
you mean: if the user view expect 10 seconds to get updated data, It will be better if I set the request interview every 8 seconds ?
adisembiring
Yes, either you poll every 8 seconds and check server side that the user is not exceeding 1 every 10 seconds (or you poll 10, and check for 12 seconds for instance...). A matter of taste. The idea is prevent a *refresh* order cancellation because of some network lag between the browser (client) and your server (I changed my sentence above since it was ambiguous and wrong).
ring0
+1  A: 

You need JavaScript for this. With JavaScript you can fire asynchronous HTTP requests to the server using XMLHttpRequest, retrieve and process the response in either HTML, XML or JSON format and traverse and modify the HTML DOM tree accordingly based on the information in the response. This technique is since about a decade ago also called with a buzzword Ajax.

Assuming that you're completely new to the client side techniques, I suggest to put Java apart for a while and take time to learn JS, HTML DOM and Ajax. There are several online tutorials and references for this, the well known (and most critized) one being w3schools: JS tutorial, HTML DOM tutorial, Ajax tutorial. To go a step further, you may consider to learn jQuery to ease all the verbose asynchronous request handling and HTML DOM traversing and last but not least to avoid entering the hell of crossbrowser compatibility issues.

In the server side, you could create a simple Servlet class which listens on ajaxical requests and returns response accordingly. The response is best to be returned in a format which JavaScript can easily parse and interpret. Currently, JSON is a very popular format. In Java there are tools to convert between fullworthy Javabeans and a JSON string, like Google Gson.

Firing an ajaxical request every interval is also called polling. You can use JS setInterval() (or setTimeout()) function to execute another function in the given intervals (or timeout). To put the pieces together, here's a kickoff example based on jQuery and a servlet which requests the server's time every second and updates the HTML DOM element.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 3850278</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
        <script>
            $(document).ready(function() {
                setInterval(function() {
                    $.get('timeservlet', function(data) {
                        $('#time').text(data);         
                    });
                }, 1000); // 1000 milliseconds = 1 second.
            });
        </script>
    </head>
    <body>
        <p>Current date/time is: <span id="time"></span>
    </body>
</html>

Here's how the doGet() method of the servlet which is mapped on an url-pattern of /timeservlet can look like:

response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Date().toString());

Related questions:

BalusC
Why is w3schools most critisized?
The Elite Gentleman
@The: It has here and there some "poor practices". It does not tech you programming "the right way". It does not teach about web semantics. It should more be seen as a reference, not as a tutorial.
BalusC
The Elite Gentleman
@The: correct :) Those are critized by purists whenever one attempt to reference them as a tutorial.
BalusC