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 ?
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 ?
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.
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)
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"></script>
<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());
setTimeout()